Light Kun
Light Kun

Reputation: 147

Problem in validation of numeric in textboxes asp.net

My Question is how I'll be able to set a RegularExpressionValidator to numeric and special character like (),-.? Like (02)1234-123:

check my codes below.. its running properly.. only for numeric..

 &nbsp;<asp:TextBox ID="txtManual" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="revNumericValidator" runat="server" 
ValidationExpression="^[0-9]*$" ControlToValidate="txtManual" ErrorMessage="Must be       Numeric" />

Upvotes: 0

Views: 742

Answers (1)

Joel Lee
Joel Lee

Reputation: 3854

Change your validation expression to:

"^\(\d+\)\d+-\d+$"

This will match strings like (02)1234-123, but it will also match strings like (1212)1-123456, because it will match any number of digits in each group.

To limit the number of digits in each group you can use {n} where n is the number if characters to match. For example:

"^\(\d{2}\)\d{4}-\d{3}$"

Here is a link to a "cheat sheet' for regular expressions.

http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet

Upvotes: 1

Related Questions