Reputation: 1
Hi i want to validate my Textbox in ASP.NET using regular expression validator.
My Textbox should take only numbers and special characters.
What is the validation expression I can use?
Please help me.
Upvotes: 0
Views: 29317
Reputation: 2548
try this pattern [-+\d()]+
. I think +
is also include sometimes in phone numbers
Upvotes: 0
Reputation: 115
The Expression for a valid phone number like (999) 999-9999 would be:
^[\d\s\(\)\-]+$
This means it has occurences of 0-9 digits the () parenthesis the minus and spaces. They all could occur one or more times so (()3-4 4) would be correct as well. But since you want the spl characters to occur everywhere this would be appropriate.
Maybe use the property validation which is in the System.ComponentModel.DataAnnotations namespace. The property would look like this:
[RegularExpression(@"^[\d\s\(\)\-]+$")]
public string NumbersAndSpecialChars { get; set; }
An Asp-Control using the expression would look like this (like Rob answered):
<asp:TextBox ID="TextBox1" runat="server"
Style="z-index: 100; left: 259px; position: absolute; top: 283px"
ValidationGroup="check"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="You must enter a phone number in the form of (999) 999-9999."
Style="z-index: 101; left: 424px; position: absolute; top: 285px"
ValidationExpression="^[\d\s\(\)\-]+$" ValidationGroup="check"/>
Upvotes: 1
Reputation: 1591
Only numbers can enter into that Textbox
We can use Regular expression validator for this: In the validation expression property keep ^\d+$.
<asp:TextBox ID="TextBox1" runat="server"
Style="z-index: 100; left: 259px; position: absolute; top: 283px"
ValidationGroup="check"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Please Enter Only Numbers"
Style="z-index: 101; left: 424px; position: absolute; top: 285px"
ValidationExpression="^\d+$" ValidationGroup="check">
</asp:RegularExpressionValidator>
Upvotes: 2