Reputation: 11
In asp.net, I want to use regularexpressionvalidator for phone number. requirement is only to allow numbers and Dash (-)
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPhone"
ErrorMessage="Not a Valid Phone Number" ValidationExpression="\d*">
The above code only validates numbers but not Dash(-). Like user randomly put 23333-34 (should accept) or any combination. It is not must that - will be there. It can be numbers only some times. Please help. Thanks.
Upvotes: 0
Views: 1306
Reputation: 7339
Must start with one or more number, followed by a possible dash and must end with another number.
^\d+\-?\d+$
Upvotes: 1
Reputation: 5319
Try accepting number OR dash, and dash only if it is not followed by another dash.
(\d|[-](?![-]))*
Upvotes: 0