Click here to Skip to main content
14,331,160 members
Rate this:
Please Sign up or sign in to vote.
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?
Rate this:
Please Sign up or sign in to vote.

Solution 4

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

set textbox maxlenght =10
and use
<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server"" xmlns:asp="#unknown">
ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator"
ValidationExpression="\d+"/asp:RegularExpressionValidator
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

See here[^].
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100