0

How do i restrict users to type only 4 numeric values in a textbox in asp.net? I need to use the regular expression validation please help

3
  • 1
    i don't know what to do, i am really novice in regular expressions Commented Jul 7, 2012 at 11:24
  • That's fair enough, but SO helpers are just that, helpers, they won't tend to do tasks in their entirety. Please have a look into some basic REGEX tutorials and attempt something, then we can help. [EDIT - @buring_LEGION has just completely blown me out of the water with an answer, so...] Commented Jul 7, 2012 at 11:27
  • sorry for a bad question (Maybe) i am new in here Commented Jul 7, 2012 at 11:29

4 Answers 4

1

Try this for asp.net

<asp:TextBox runat="server" ID="txt" />

   <asp:RegularExpressionValidator runat="server" 
        ID="reg1" ValidationGroup="save" ControlToValidate="txt"
             ValidationExpression="^\d{4}$" ErrorMessage="Not 4 digits" />

<asp:Button runat="server" ID="Btn" Text="Clicker" ValidationGroup="save" />
Sign up to request clarification or add additional context in comments.

1 Comment

It would accept blank and four digits only, try it by entering any other than these values.
0

Pete its very simple. in validation expression just type ^\d{4}$. you will be done.

3 Comments

where is validation expression?
how have you done so far? you got a textbox, and a regular expression validator on ur webform. right? in regular expression validation properties, find ValidationExpression and just typpe " ^\d{4}$ " without quotes and u'll be done
we can write our own regex too. For onfo visit this link
0

if this regex is match ^\d{4}$ then user input only 4 digit

1 Comment

my regex match lonely 4 digit in string, if string contains something else regex does not match
0

The below code validate from 0 digit to 4 digit. if you want exactly 4 digit then please follow burning_LEGION's example ^\d{0,4}$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.