Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want regular expression to validate mobile no in form
91-9876657654
or
9887676545
or
919887676545 (without -)
or
1-6314555173
i.e i can enter country code or i can not but no after country code
should be 10 digit.If not entering code just 10 digit no but not less
than 10 digit.
Posted
Comments
OriginalGriff 20-Oct-10 5:36am    
What have you tried?

This regex is a final solution to your problem which validates all your mobile numbers.

(\d{1,2})?\-?\d{10}

Which represents.

(\d{1,2})? - 0 OR 1 repetaion of min 1 or max 2 digit number ( passes country code)

\-? - O OR 1 repetation of hypen

\d{10} - any 10 digit number ( passes mobile number )

Hope it helps.

Please vote and Accept Answer if it Helped.
 
Share this answer
 
set textbox maxlenght =10
and use
<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server"" xmlns:asp="#unknown">
ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator"
ValidationExpression="\d+"/asp:RegularExpressionValidator
 
Share this answer
 
v2
See here[^].
 
Share this answer
 
How many Countrycodes could be entered?

First of all, i would remove all special chars like '-', to get a clean input.
Then i would do a check to the length. If the length is over 10 digits, then its a countrycode included. Then you check again the Regex with Countrycode. And if not, use the second regex.

Pseudocode:
if input.length > 10
  Regex = ^\d{1,2}\d{10}$
else if input.length = 10
  Regex = ^\d{10}$
else
  crap input
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900