Solved

regex on 4 digits numeric textbox validation

Posted on 2014-03-23
4
408 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
Comment Utility
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
Comment Utility
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now