Solved

regex on 4 digits numeric textbox validation

Posted on 2014-03-23
4
426 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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