2

i am trying to do Validation check on mobile number , somewhat similar to what gmail had implemented.

Gmail signup page link

but variation among the phone number across country is to much, so finding it difficult to frame a regex for this.

i looked upon some of the q's here , but they work for some country or a particular country, please mention if i missed.

question in SO

mainly i want to achieve what gmail has done with the mobile number.

Community
  • 1
  • 1
Aditya
  • 120
  • 3
  • 11
  • Why don't you look how Google did it? – Reeno Sep 12 '13 at 09:10
  • b'coz it difficult to look. hope u got the point, i was looking for someone who has done this before, i wasn't interested in reinventing the wheel. but seems like, it's very rare. – Aditya Sep 12 '13 at 09:33
  • This is actually not a programming issue. What you need is to find someone who compiles and *maintains* a database of valid phone numbers around the world (and is willing to share it). – Álvaro González Sep 12 '13 at 09:42
  • Sorry for the typo: I meant valid phone number *formats*. – Álvaro González Sep 13 '13 at 10:45

4 Answers4

4

In the page you provided, google uses ajax to check those e-mails. Look at the Request content:

{"input01":{"Input":"RecoveryPhoneNumber","RecoveryPhoneNumber":"+44 12345678","RecoveryPhoneCountry":"GB"},"Locale":"pl"}

My guess is they don't have any magical universal regex - they probably have the whole database of regexes - each matching phone numbers in every country. My guess is you can do the same, but you will have to work on creating such database (if no one already did).

You can also cheat your way there and try to connect to their ajax service and make requests on your own to their checker. This would be an easy way, but really not reliable and probably not even legal.

EDIT: There are a lot of pages where you can find examples. The hard part is to gather all of the regexes.

Community
  • 1
  • 1
Kelu Thatsall
  • 2,494
  • 1
  • 22
  • 50
  • Thanks Kelu , for all the valuable information provided, i think , i should better go and create my own lib of regex to achive this. – Aditya Sep 12 '13 at 09:31
3

Don't know if you've written your own lib for mobile number validation, but now we have libphonenumber. It is, indeed, provided by Google, and allows you to validate, parse and format phone numbers. It is for Java, c++ or Javascript, but there's also fork for PHP.

At this point, to achieve something similar to form on Google's signup page you'll need to connect lib with some coded input (to provide flags and country codes to choose from). intl-tel-input was designed especially for this. It is basically:

A jQuery plugin for entering and validating international telephone numbers. It adds a flag dropdown to any input, which lists all the countries and their international dial codes next to their flags.

and it uses Google's libphonenumber mentioned above. So, leave out your regexes :D

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
Paweł Tomkiel
  • 1,974
  • 2
  • 21
  • 39
0

The giants are making proper Regex , but after getting the country code over their database you can find this running over their code to find out how they builds up country code and flags

"in":{code:"91",name:"India",imgPosition:"-1694px"},id:{code:"62",name:"Indonesia",imgPosition:"-1958px"},ir:{code:"98",name:"Iran",imgPosition:"-2013px"},iq:{code:"964",name:"Iraq",imgPosition:"-649px"},ie:{code:"353",name:"Ireland",imgPosition:"-1969px"},
internals-in
  • 4,798
  • 2
  • 21
  • 38
0

For International Mobile number, use below validation.

^+\d{1,5}\d{6,11}$

(+)symbol - is mandatory

d{1,5} - for ISD Code

d{6,11} - for phone number

indian mobile number can be check with ^+91[6789]\d{9}$

Rajesh
  • 9
  • 2