Page 1 of 1

Validating UK telephone number formats

Posted: Sun Jun 07, 2009 8:59 am
by jordmaz82
Good afternoon guys,

I am trying to validate a telephone number in a mailer script I have written. I have used the following regular expression with preg_match to try and achieve this but it does not work:

Code: Select all

 
<?php
    $to = "mail@*****.co.uk";
    $subject = "Message sent from ****.co.uk";
    $name_field = $_POST['name'];
    $email_field = $_POST['email'];
    $tel_field = $_POST['telnumber'];
    $message = $_POST['message'];
    $body = "From: $name_field\n Email: $name_file\n Tel number: $tel_field\n Message:\n $message";
    $reg = '^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$';
    if(preg_match($reg, $tel_field))
    {
    //mail($to, $subject, $body);
    header('Location:success.html');
    }
    else
    {
    header('Location:failure.html');
    }
?>
 
I have run the regular expression against an online tester and it seems to work successfully but once I implement it within the script it just redirects the user to the failure page.

I hope you guys can help me because it is starting to drive me wild! I am sure that it is something simple that I am missing but I cant see the wood for the trees.

Thanks in advance

Re: Validating UK telephone number formats

Posted: Sun Jun 07, 2009 9:23 am
by Weirdan
regular expression need start and end delimiters matched:

Code: Select all

 
$re = '/a/'; // matches a
$re = '%b%'; // matches b
$re = '#q#'; // matches q
 

Re: Validating UK telephone number formats

Posted: Sun Jun 07, 2009 1:32 pm
by jordmaz82
Thanks that is great. Apologies for the formatting - I will make sure I get it right in the future.

Once again, thanks