Solved

validate Textbox input for decimal values in c#

Posted on 2009-03-30
3
4,055 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

623 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