Solved

validate Textbox input for decimal values in c#

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I get the unique ID after insert? 87 90
WPF issue with Trigger 2 67
asp.net mvc5 3 37
SQl help with selection 14 44
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now