Validating UK telephone number formats

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jordmaz82
Forum Newbie
Posts: 2
Joined: Sun Jun 07, 2009 8:49 am

Validating UK telephone number formats

Post 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
Last edited by Weirdan on Sun Jun 07, 2009 9:19 am, edited 1 time in total.
Reason: added [code=php] tags
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Validating UK telephone number formats

Post 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
 
jordmaz82
Forum Newbie
Posts: 2
Joined: Sun Jun 07, 2009 8:49 am

Re: Validating UK telephone number formats

Post by jordmaz82 »

Thanks that is great. Apologies for the formatting - I will make sure I get it right in the future.

Once again, thanks
Post Reply