Validating UK telephone number formats
Posted: Sun Jun 07, 2009 8:59 am
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:
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
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 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