Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Mobile Number with or without Country Code

I need to validate a text field in my registration form for which I want a Regex.

The textbox will accept a mobile number like 9999999999(10 digit - starting with 7 or 8 or 9). The user may alternatively write +919999999999, where +91 is country code (+91 for India, but can accept any other like +110 or +52), or he may also write 09999999999 (first 0 for own country)

Here, the user have 3 choices,

  1. adding the mobile number with country code
  2. simply without country code or 0 prefixed
  3. With a zero prefixed in mobile number.

Though not required, my page is in asp.net, and I am using built-in regular expression validator.

like image 582
Cyberpks Avatar asked Oct 12 '12 11:10

Cyberpks


People also ask

How do I validate a mobile number?

Mobile Number validation criteria:The first digit should contain numbers between 6 to 9. The rest 9 digit can contain any number between 0 to 9. The mobile number can have 11 digits also by including 0 at the starting. The mobile number can be of 12 digits also by including 91 at the starting.

How do I validate a mobile number in Google forms?

Because Checkbox questions can contain more than one answer, you can validate the number you want to allow. Choose “Select at Least,” “Select at Most,” or “Select Exactly” in the first drop-down box. Then enter the corresponding number to the right.


1 Answers

From what I can see, this should work. The prefix is optional and is stored into the first match group, with the main number going into the second group.

^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$

But if you can give us some test cases for each, it'd help us in giving you a working regex for what you want.

Props to SchlaWiener in the comments for the correct limit on the country code length.

like image 195
Joe Avatar answered Oct 08 '22 14:10

Joe