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
Solved

regex on 4 digits numeric textbox validation

Posted on 2014-03-23
4
413 Views
Last Modified: 2014-03-23
i tried to validate textbox with numeric 4 digit only, but not quite working

Regex regex = new Regex(@"(^|\s)[0-9]{1,4}($|\s)");
        if (!regex.IsMatch(txtCustomerCode.Text))
        {
            lblSavingErrorMessage.Text = "Please enter customer code in numeric format only";
            lblCustomerCodeRequired.Visible = true;
            validationcounter -= 1;
        }
0
Comment
Question by:doramail05
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
doramail05 earned 0 total points
ID: 39949373
got it worked with :

<asp:Label ID="lblCustomerCodeRequired" Text="*" ForeColor="Wheat" Visible="false" runat="server" />
           <asp:RegularExpressionValidator runat="server" id="rexNumber" controltovalidate="txtCustomerCode"
           validationexpression="^[0-9]{4}$" ForeColor="Pink" errormessage="Please enter customer code in numeric format only" />
0
 
LVL 1

Author Closing Comment

by:doramail05
ID: 39949374
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39949376
Not sure in which was it wasn't working, but I can see a value like "blah 1 blah" would be allowed. To only allow whitespace before and after the number, you can change the pattern to this:

@"^\s*[0-9]{1,4}\s*$"

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39949379
I see you appear to have solved it (great!), but I should point out that your accepted solution doesn't allow spaces before or after the number, and requires exactly 4 digits. If you think my solution is better, we can reopen the question...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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