Jump to content

Telephone number validation


BCAV_WEB

Recommended Posts

Can anyone tell me why this bit of coding isn't working.

 

I want a a form to validate the telephone number for the UK and been having a bash around with various ways to code this, with no success so far.

 

I started with FILTER_VALIDATE_INT, but found out that "0" wouldnt be taken so I added FILTER_FLAG_ALLOW_OCTAL. But then it seems that the spaces, dashes wouldn't be taken as well as a limit on 10 characters occured.

 

So I tried to write myself a small function that used an array to validate the styles etc... But this seemed to allow any string character. So I moved on to preg_match, but I can't seem toget my coding to work.  :'( It's driving me mad now, probably just need a break  :P But any help will be much appericated!

 

[

 

if (!empty($_SESSION['add_customer_form'] ["telephone_no"]))

{

 

//if (!is_numeric($number))

//if(!filter_var($int, ))

$number = $_POST["telephone_no"];

if(!preg_match("/^(+)?(([0-9]+)-?s?)*([0-9]+-[0-9]+)*([0-9]+)*$/", trim($number)))

  {

  $_SESSION['add_customer_errors']["telephone_no"] = ("$number Sorry, but you′ve entered an incorrect fromat");

}

 

 

//if( !preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $_SESSION['add_customer_errors']["telephone_no"]) )

//{

  /// $number = $_POST["telephone_no"];

//$_SESSION['add_customer_errors']["telephone_no"] = ("$number Sorry, but you′ve entered an incorrect fromat");

  //}

}

]

Link to comment
Share on other sites

Country code - necessary only when dialing to phones in other countries. In international usage, telephone numbers are prepended with the country code preceded by a "+", and with spaces in place of hyphens (e.g., "+XX YYY ZZZ ZZZZ"). This allows the reader to choose which Access Code (also known as International Dialing Digit) they need to dial from their location. However, it is often quoted together with the international access code which must precede it in the dial string, for example "011" in NANP countries (including Canada, Bermuda, and the United States): "011-XX-YYY-ZZZ-ZZZZ", or "00" in most European countries: "00-XX-YYY-ZZZ-ZZZZ". This can cause confusion as a different Access Code may be used where the reader is located. On GSM networks, "+" is an actual character that may be used internally as the international access code, rather than simply being a convention.

 

when working with regex's always important to look at how to fit the pattern

Hardest part about this pattern is the initial +/011/00 country designator

but i think i worked it out (but no () parens around area code for local)


<?php
$numbers=array(
'234-567-8901',
'+01 234 567 8901',
'011-01-234-567-8901',
'00-01-234-567-8907',
'0123456789'
);



foreach($numbers as $val)
{
echo $val . " is ".
	(preg_match('@^((\+|((011|00)(\s|-)))\d{2,2}(\s|-))?\d{3,3}(\s|-)\d{3,3}(\s|-)\d{4,4}$@',$val)?'':'not ')
	. "ok<br />\r\n";
}
?>

 

Wow, that regex is mad looking

Link to comment
Share on other sites

I would suggest not validating the "format" of the phone number and instead validate that the phone number has the correct number of digits. Then you can store just the numbers in your database and use a consistent manner in which to display the phone number.

 

For example (using US numbers), I would allow the user to enter in any value that consisted of 10 numeric digits OR 11 digits if the first number was a 1:

- 123-465-7890

- (123) 456-789

- 123.456.7890

- 1-123-456-7890

 

Sampe code for validating

function validPhone($phone)
{
    //Remove all non digits
    $phone = preg_replace('/[^\d]/', '', $phone);
    //Test number of digits
    if(preg_match('/^1?\d{10}$/', $phone) !=1 )
    {
        return false;
    }
    return $phone;
}

 

That function will return false if the number doesn't contain a valid number of digits, else it returns just the digits of the number. So, if validation passes I store just the numeric digits of the entered value. Then, whenever I need to display the value on the page I would have a function to format the phone numbers in a consistent format.

function formatPhone($phone)
{
    if(!$phone) { return false; }
    $format = (strlen($phone)==10) ? '(\2) \3-\4' : '\1 (\2) \3-\4';
    return preg_replace('/(\d)(\d{3})(\d{3})(\d{4})/', $format, $phone);
}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.