0

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.

2
  • Hi, how does your web form look like so far ? Commented Feb 17, 2011 at 9:38
  • thing is my text box will take Phone Number So Phno have contain few spl character ..like..() and - along with numarics so i need number and Spl chracter onlymy textbox have to take pls try to help me.. Commented Feb 17, 2011 at 9:44

3 Answers 3

2

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>
Sign up to request clarification or add additional context in comments.

5 Comments

Only numbers and special characters.
i wouldn't use the "+" sign, as santhosh didn't specify there should be a value inserted
thing is my text box will take Phone Number So Phno have contain few spl character ..like..() and - along with numarics so i need number and Spl chracter onlymy textbox have to take pls try to help me..
regular-expressions.info Start there, spend an hour or two and figure out how they work. This will save you a lot of time in the long run.
Unless you know for sure that your audience is 100% local you should allow international numbers like +46-8-1234 5678 and if you are going to actually use the phone number (why else are you asking? Oh yeah, right, "I am only following my orders") perhaps you should also ask in which time zone the customer is.
1

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"/>

5 Comments

its not working,,pls try to give validation Expression for reguler Expression control
couldn't you copy and paste the expression to the answer of Rob? You just have to escape the expression differently because its asp not c#
would this > (999) 999-9999 be a valid number?
yes i want like this only but my spl character might be place any where in that numarics
If you only need to accept nine-digit phone numbers where optional special characters may appear before the first digit, after the third digit, and after the sixth digit the most permissive regular expression would be [^\d]*\d{3}[^\d]*\d{3}[^\d]*\d{4}. All this cares about is that there are 10 digits in the input.
0

try this pattern [-+\d()]+. I think + is also include sometimes in phone numbers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.