Re: Phone number validation

From: Date: Sun, 12 Mar 2006 20:01:34 +0000
Subject: Re: Phone number validation
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-231807@lists.php.net to get a copy of this message
On Sun, Mar 12, 2006 at 12:09:42PM -0700, Paul Goepfert wrote: > Hi all, > > I am trying to validate phone numbers in a web form that I have > created. I am using a regular expression to validate the phone > number. It seems when I enter the phone number in the following ways > I get errors > > (123) 456 7890 > 123 456 7890 > (123) 456 - 7890 > 123 456-7890 Dont forget: 123.456.780 1-123-456-7890 > > ... > > $validPhone = "^([0-9]{3}[ ]*)?[0-9]{3}[ ]*[0-9]{4}$"; > > By the way The phone numbers are in US format. With US phone numbers I always use this approach to avoid what format people use to enter the phone: - Remove any non digit $check_phone = preg_replace('/[^0-9]/', '', $phone); - If $check_phone strlen < 10.. invalid - if $check_phone strlen == 11 && first char == '1'.. remove it - if $check_phone strlen == 10, seems ok - possible area code check with prefix check (if have db to support that) I would even go as far as storing the phone striped of any formating and only format it when you display it. The other option would to make three seperate input fields in the form and join them together in your script: (<input name="phone[area]">) <input name="phone[code]">-<input name="phone[num]"> Curt. -- cat .signature: No such file or directory

« previous php.general (#231807) next »