Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

validate Textbox input for decimal values in c#

Posted on 2009-03-30
3
Medium Priority
?
4,060 Views
Last Modified: 2013-11-25
Hi,
I want to validate user input for deicmal values upto two decimal places. I dont want to  use Ajax Mased Input. Any idea
thanks
0
Comment
Question by:mmalik15
3 Comments
 
LVL 46

Accepted Solution

by:
tbsgadi earned 2000 total points
ID: 24019201
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24019235
You could add a custom validator to the page, and handle the ServerValidate event
 
  protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) {
    decimal value;
 
    // Test for decimal
    if (!decimal.TryParse(args.Value, out value)) {
      args.IsValid = false;
      return;
    }
 
    // Test for 2 decimal places
    args.IsValid = ((value * 100) % 1 == 0);
  }

Open in new window

0
 

Expert Comment

by:highlandgirl
ID: 24702942
If you don't require users to type the decimals but only want to prevent them from inputting more than 2, you can use a CompareValidator, set the ControlToValidate to your textbox, set the Operator to DataTypeCheck and Type to Currency.
It will allow numbers like 123 | 123.0 | 123.00 | 123.99
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question