Solved

regex on 4 digits numeric textbox validation

Posted on 2014-03-23
4
416 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AJAX ModalPopupExtender will Not Hide in Asp.net 1 35
Add a Condition in ASP Repeater 6 45
vb.net convert long time to mm:ss 23 21
Multi-Column Repeater 3 26
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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