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?
chokkaStudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
what is the difference between digits and units?
Please show a sample of valid and invalid entries
0
chokkaStudentAuthor Commented:
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.
0
wilcoxonCommented:
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.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

David Johnson, CD, MVPOwnerCommented:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-3.3.1.min.js"></script>
</head>

<body>
    
    <form id="form1" runat="server">
        <div><asp:TextBox ID="TextBox1" runat="server" />
           
<br />
<asp:RegularExpressionValidator ID="Regex1" runat="server" ValidationExpression="\d{1,3}\.\d{1,2}$"
ErrorMessage="Please enter valid integer or decimal number with 2 decimal places from 0-999.99."
ControlToValidate="TextBox1" />
        </div>
    </form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chokkaStudentAuthor Commented:
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.
0
chokkaStudentAuthor Commented:
Also i am using <Input> syntax. Instead of ValidationExpression, i am using the attribute : pattern
0
wilcoxonCommented:
I'm guessing there isn't a way to do that but, as I said, I don't know ASP (just regexes).
0
chokkaStudentAuthor Commented:
Anyways, for now i am good. Thanks for helping me !!!!
0
chokkaStudentAuthor Commented:
@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

0
wilcoxonCommented:
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}).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.