0

I am using the following REG-EX validator to validate a text box for accepting only two digits after a decimal point .

<asp:RegularExpressionValidator Display="Dynamic" ID="regexp" runat="server" ControlToValidate="regexptest" ValidationGroup="regexptest" ValidationExpression="^\d+(\.\d\d)?$">

But I am able to enter more than two values after decimal point.

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Ankur
  • 1,023
  • 7
  • 27
  • 42
  • This has already answer before in several thread. Take a look at this http://stackoverflow.com/questions/308122/simple-regular-expression-for-a-decimal-with-a-precision-of-2 – Debajit Mukhopadhyay Jul 02 '13 at 06:19
  • @DebajitMukherjee : None is working. I tried. – Ankur Jul 02 '13 at 06:36
  • You mean you cannot type more than 2 numbers after the decimal? Or the interface doesn't accept? – 7alhashmi Jul 02 '13 at 06:38
  • @7alhashmi : I want something like this "123.55". Right now in my aspx file I have applied a – Ankur Jul 02 '13 at 06:40
  • Try this out http://stackoverflow.com/questions/1014284/regex-to-match-2-digits-optional-decimal-two-digits – pixelbyaj Jul 02 '13 at 06:41
  • I guess you are trying to prevent the user to enter more than two numbers after the decimal. You can do it using AJAX toolkit. Try this: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/FilteredTextBox/FilteredTextBox.aspx – 7alhashmi Jul 02 '13 at 06:44
  • Note that either of the two current answers (MicrosoftDJ's and 7alhashmi's) will only work if the decimal separator in the current locale is the period. – user Jul 02 '13 at 07:24

3 Answers3

6
^\d+(\.\d{2})?$ 

valid Examples are - 23, 23.12
Invalid example- 23.2, 23.123

Edit:

Please have a look at following links to restrict the user from entering more than two numbers after decimal-

http://www.mredkj.com/tutorials/validate2.html

http://www.mredkj.com/tutorials/validate2.js

Microsoft DN
  • 9,706
  • 10
  • 51
  • 71
  • It works. But,how can I do the same using ajax. That is even if the user tries to enter he is not able to enter more than 2 characters after decimal – Ankur Jul 02 '13 at 07:02
  • You can use a function in javascript or same to restrict the user to enter more than two numbers after decimal point. – Microsoft DN Jul 02 '13 at 07:13
3

Either @Microsoft DJ's answer or @7alhashmi's answer should work fine provided that the decimal separator is the period and not some other character. This holds for US English, but other languages and locales use different separators so for a truly internationalized solution, you cannot rely on that being the case. This makes it difficult to do proper validation using only a regular expression.

Fortunately, there is another possibility: you can leverage decimal.TryParse() with a culture specifier together with Math.Round(). For example, if the user's culture is fr-FR then you could do something like:

string value = "1234,56";
NumberStyles style = NumberStyles.AllowDecimalPoint;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR"); // or whatever
decimal number;

if (!Decimal.TryParse(value, style, culture, out number))
{
    failValidate(FailReason.CannotParse); // Could not parse as a number
}
else if (number != Math.Round(number, 2))
{
    failValidate(FailReason.TooManyDecimals); // More than two decimals provided
}
else
{
    succeedValidate(); // Number was valid and has at most two decimals
}

Of course, this technique is only really useful on the server side (where you should be doing all the validation that you rely on anyway; client-side validation is a convenience for the user, not something to depend upon for security or input validation).

Using Math.Round() as above will catch the case of the user entering 12.001, but 12.000 will be accepted since it is equal to 12.00 or for that matter 12. Depending on your usage scenario this may or may not be acceptable.

Community
  • 1
  • 1
user
  • 6,897
  • 8
  • 43
  • 79
1

Try this:

<asp:RegularExpressionValidator Display="Dynamic" ID="regexp" runat="server" 
     ControlToValidate="regexptest" ValidationGroup="regexptest" 
     ValidationExpression="^\d+(\.\d{1,2})?$">
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
7alhashmi
  • 924
  • 7
  • 24
  • Try this http://stackoverflow.com/questions/1014284/regex-to-match-2-digits-optional-decimal-two-digits – pixelbyaj Jul 02 '13 at 06:41
  • How can do the same using ajax. that is even if the user tries to enter he is not able to enter more than 2 characters after decimal – Ankur Jul 02 '13 at 06:57