Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In asp.net, I want to use Regular Expression Validator for number.

Requirement is only to allow 3 numbers and Dash (-)

For Example:
number any from 0 to 9
123-123-123
Posted
Comments
Kenneth Haugland 17-May-14 8:30am    
\d{3}\- ???

try this :


C#
^[0-9]{3}-[0-9]{3}-[0-9]{3}$



learn here
The 30 Minute Regex Tutorial[^]
 
Share this answer
 
try this:
^\d{3}(-\d{3})+$
 
Share this answer
 
^[0-9]{3}([-][0-9]{3})+$
 
Share this answer
 
If you want to allow any type of pattern as you mentioned as long as the string contains just numbers and dashes, you could use :

^[\d-]+$

or (you can explicitly escape the dash as well using \-):

^[\d-]+$

which would be used in a RegularExpressionValidator as such :

ASP.NET
<asp:regularexpressionvalidator id="YourValidator" runat="server" xmlns:asp="#unknown">
                                Display="Dynamic" 
                                ControlToValidate="Txt_ID" 
                                ValidationExpression="^[\d\-]+$" 
                                ErrorMessage="ID# - 15 Characeter Length.">
</asp:regularexpressionvalidator>
Explanation of Expression

^ # Beginning of expression
[\d-]+ # Allows one or more (+) digits (\d) or dashes (-)
$ # End of expression
Additionally, if you wanted to specify the format such as the one that your example shows, you would use :

^\d{3}-\d{3}-\d{3}$
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900