Avatar of chokka
chokka
Flag for United States of America asked on

How to have textbox regular expression validation on asp.net?

How to have textbox regular expression validation on asp.net?


I will have to allow 3 digits and units in the text box. Decimal units should not exceed more than 2 units.

How to handle that?
Regular ExpressionsASP.NETJavaScript

Avatar of undefined
Last Comment
wilcoxon

8/22/2022 - Mon
David Johnson, CD

what is the difference between digits and units?
Please show a sample of valid and invalid entries
chokka

ASKER
1.0 to 999.99 is valid

If user entered 500.000 , it will not allow user to enter more than 500.00  or round it up.

In other words .. user cant type 100.00000000 ..  decimal units should be stopped at 2 digit units.
wilcoxon

I don't know ASP but the regex would be this:
^\d{1,3}\.\d{1,2}$

Open in new window


That will require at least one digit before and after the decimal with a max of 3 before and 2 after.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
David Johnson, CD

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
chokka

ASKER
This is my solution. It works.

But .. what it happens . is that, it allows the user to enter the number as 100.000000

and then prompt a message.

Instead of doing that, is there a way to prevent the user to type more than .00 digits.
chokka

ASKER
Also i am using <Input> syntax. Instead of ValidationExpression, i am using the attribute : pattern
wilcoxon

I'm guessing there isn't a way to do that but, as I said, I don't know ASP (just regexes).
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
chokka

ASKER
Anyways, for now i am good. Thanks for helping me !!!!
chokka

ASKER
@Wilcoxon

Regex which you shared : ^\d{1,3}\.\d{1,2}$


When we enter the digit as 100 , it doesn't take it. It is expecting to enter the digit as 100.00
decimal unit should be optional as well as decimal unit should not exceed more than 2 units.

This is my current syntax :

[RegularExpression(@"\d{1,3}\.\d{1,2}$", ErrorMessage = "Invalid.")]
[Range(60.00, 120.00, ErrorMessage = "Invalid.")]

Open in new window

wilcoxon

Is there some reason that you did not give me partial credit for providing the regex (the same one that David Johnson used in his code)?

Based on your original post and your first comment, it appeared you were saying that at least one decimal digit was required.  To make it optional, change the regex to:
^\d{1,3}(?:\.\d{1,2})?$

Open in new window


Now it will accept anything from 0 to 999.99 (if 0 should not be allowed then change the first \d{1,3} to [1-9]\d{0,2}).
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23