Posted By

aristoworks on 07/01/08


Tagged

php phone number simple validation regular expressions telephone


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

jdstraughan
robe
vali29


PHP (RegEx) Telephone Number Validation


 / Published in: PHP
 

URL: http://www.thejwalker.com

A simple method of validating a telephone number using regular expressions and PHP

  1. $string = "(232) 555-5555";
  2. if (preg_match('/^\(?[0-9]{3}\)?|[0-9]{3}[-. ]? [0-9]{3}[-. ]?[0-9]{4}$/', $string)) {
  3. echo "example 2 successful.";
  4. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: maneeshnsony on August 29, 2012

Corrected the regex with more accuracy for validating a phone/mobile number

$string = "(232) 555-5555";
$expr = '/^\+?(\(?[0-9]{3}\)?|[0-9]{3})[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{4}$/';
if (preg_match($expr, $string) == 1)
{
echo "example 2 successful.";
}

You need to login to post a comment.