Solved

validate Textbox input for decimal values in c#

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

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…

776 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