Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

regex on 4 digits numeric textbox validation

Posted on 2014-03-23
4
Medium Priority
?
430 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

877 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