Second Sighting Logo

Simple UK Phone Number Validation

Posted in PHP by Tom on the August 2nd, 2007

This is a simple solution which should help a lot with accidental errors, but it’s still very easy for someone to enter a fake number if they want to. The first line of code removes anything thats not a number from a submitted post variable called ‘phone’. The second line checks the number begins with a 0, followed by 1-9 (‘00′ is not allowed as it’s the UK dialling prefix for international calls), followed by 8 or 9 other digits. The example below works with PHP 5.2.0 & above.

  1. $phone = ereg_replace('[^0-9]', '', $_POST['phone']);
  2. if (!filter_var($phone, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/")))) {
  3. echo 'Please enter your phone number correctly';
  4. }

0 Comments

Leave a Reply