user2690007
user2690007

Reputation: 11

How to get the regularexpression for Dash and Numbers

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

Answers (4)

user557597
user557597

Reputation:

My guess is something like this:

 ^ \d+ (?: - \d+ )+ $

Upvotes: 0

James
James

Reputation: 82136

You need something like

\d*(-?)

Upvotes: 0

Felipe Miosso
Felipe Miosso

Reputation: 7339

Must start with one or more number, followed by a possible dash and must end with another number.

^\d+\-?\d+$

Upvotes: 1

Adrian Salazar
Adrian Salazar

Reputation: 5319

Try accepting number OR dash, and dash only if it is not followed by another dash.

(\d|[-](?![-]))*

Upvotes: 0

Related Questions