Solved

validate Textbox input for decimal values in c#

Posted on 2009-03-30
3
4,052 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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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