Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match up to 10 digits [duplicate]

Tags:

regex

asp.net

Possible Duplicate:
How do I create a regular expression to accept not more than 10 digits?

I want a regular expression which will allow up to 10 digits in a user control having a text box. (ASP.net 3.5).

like image 247
user92027 Avatar asked Jun 24 '09 04:06

user92027


People also ask

How does regex Match 5 digits?

match(/(\d{5})/g);

How do you repeat a regular expression?

A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.

What is '?' In regular expression?

'?' matches/verifies the zero or single occurrence of the group preceding it. Check Mobile number example. Same goes with '*' . It will check zero or more occurrences of group preceding it.


2 Answers

^[0-9]{1,10}$ or ^\d{0,10}$

like image 163
SO User Avatar answered Sep 21 '22 15:09

SO User


Add regular expression validator with your textbox:

<asp:TextBox ID="tb" runat="server" MaxLength="10" />
<asp:RegularExpressionValidator ID="rvDigits" runat="server" 
   ControlToValidate="tb"  Text="*" Display="Dynamic" 
      ValidationExpression="^\d{0,10}$" />

A better approach will be to use jQuery and jquery.numeric plugin!

like image 35
TheVillageIdiot Avatar answered Sep 18 '22 15:09

TheVillageIdiot