0

Possible Duplicate:
A comprehensive regex for phone number validation

I'm trying to put together some regex patterns for telephone numbers, however my regex experience is virtually nil.

Firstly here's where I'm at for home phone numbers

^0[12]\d{8,9}$

Works great but what I'd like to do is allow whitespaces (up to 2).

Any regex gurus out there?

Community
  • 1
  • 1
dotnetnoob
  • 10,783
  • 20
  • 57
  • 103
  • any particular country's phone number format? or do you need to allow international numbers? – SDC Dec 03 '12 at 17:01
  • 2
    When it comes to phone numbers, what I like to do is just let people enter whatever symbols they want, wherever they want them. For example in the US there are several common formats, eg: `xxx-xxx-xxxx`, `(xxx) xxx-xxxx`, `xxx xxx xxxx` and others. It's easier just to strip any non-numeric character and ensure the result has the correct number of digits. – NullUserException Dec 03 '12 at 17:02
  • 1
    if you're looking for UK phone numbers (which it looks like from your example), there's a good answer here: http://stackoverflow.com/questions/32401/validate-a-uk-phone-number (note - the second answer, not the accepted answer which is rubbish) – SDC Dec 03 '12 at 17:02
  • On problems like this, expect that it is a solved problem and that there is undoubtedly existing code to do it. – Andy Lester Dec 03 '12 at 17:12

2 Answers2

1

I suggest you validate phone number by just stripping non-digits. And verify by sending a confirmation code as sms. I validate subscribers phone numbers using twilio. I send a code number to that number and user have to enter it to verify. Thats the verification process. And before inserting it into database I use following code.

$dnum = preg_replace('/\D+/','', $num);
if(preg_match('/^[\d()\s-]+$/', $num) 
    && in_array(strlen($dnum), array(7,10,11,13)){
// valid phone number
}else{
// invalid phone number
}

What this code do is,

  1. Strips all the non-digit characters
  2. Check the allowed characters. Only ()- are allowed as non-digit characters.
  3. Check the length after stripping. It should be either 7 or 10 or 11 or 13.
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Or `/^[\d\s()-]+$/` to remove unnecessary escapes; although I would just check the length of `$dnum` (and also store `$dnum` in the database) – NullUserException Dec 03 '12 at 17:08
  • Thanks for pointing out the error in my regex. In fact I store the `$dnum` in db. and keep `$num` as `displayPhone` property. – Shiplu Mokaddim Dec 03 '12 at 17:11
  • I don't think that's such a great idea because you now have two fields that contain (essentially) the same data. Not only does this mean you have redundant info, but you also have to make sure changes to one are reflected in the other. I'd just store `$dnum` and when displaying, format it the way I like. This way all phone numbers will also have a consistent format. – NullUserException Dec 03 '12 at 17:20
  • @NullUserException Ah. Right you are. Its really redundant. Its really great that not only asker gets benefited but also answerers. – Shiplu Mokaddim Dec 03 '12 at 17:24
0

Example in PHP in which the remaining string does only consist of digits and white space characters.(For US)

$output = preg_replace('/[^\d\s]/', '', $str);
Bobb Dizzles
  • 533
  • 3
  • 12