You are here: Re: US phone number validation « PHP Programming Language « IT news, forums, messages
Re: US phone number validation

Posted by Jambalaya on 04/14/06 04:06

JackM wrote:
> I'm still working on validating the phone numbers that are entered on a
> form but have come across a problem I don't understand how to fix. I can
> handle most instances when it's in regular US formats (555-555-5555 or
> (555) 555-5555 or 555 555 5555) but I'm having trouble when the entry is
> ten consecutive numbers with nothing else (5555555555). Not only does it
> not validate, but it shows as 2147483647 when I call the variable to
> check to see how it is set. I know I could break the form up into three
> values as in <input name="phone[area]"> <input name="phone[code]"> and
> <input name="phone[num]"> to eliminate that happening but I don't like
> the look of that and I know there's a better way that I haven't found.
>
> I tried googling for information on a preg_replace statement that could
> do what I need (check the phone number and add a space between the 3rd
> and 4th and the 6th and 7th digits if there isn't one there already) but
> I haven't been successful.
>
> What do I need to do to handle this?

This regex has worked pretty well for me. You can comment out the last
seperator and extension line if you don't need that.

$rgx = '/^'; //beginning of string
$rgx .= '(?:\([2-9]\d{2}\)\ ?'; //either (200-999) with optional space
$rgx .= '|'; //or
$rgx .= '[2-9]\d{2}[- \.]?)'; //200-999 with optional seperator '- .'
$rgx .= '[2-9]\d{2}'; //middle 4 digits 200-999
$rgx .= '[- \.]?'; //optional seperator '- .'
$rgx .= '\d{4}'; //last 4 digits 0000-9999
$rgx .= '[- \.]?'; //optional seperator '- .'
$rgx .= '(?:x|ext)?\.?\ ?\d{0,5}'; //optional extension
$rgx .= '$/'; //end of string

if (!preg_match($rgx, $userinput)){
$this->addError('This must be a valid phone number.');
}

If your variable is cast as an integer then the largest integer value
PHP can handle is 2147483647. PHP generally automatically switches to
float representations if your value is 2^32 or larger.[1] I store it as
a string to avoid problems of that sort...


[1]<url:http://us3.php.net/manual/en/language.types.integer.php>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация