Regex for telephone numbers

Dear List -

Thank you for all your help in the past.

Here is another one....

I would like to have a regex which would validate that a telephone
number is in the format xxx-xxx-xxxx.

Thanks.

Ethan

MySQL 5.1 PHP 5 Linux [Debian (sid)]



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ethan Rosenberg [ Do, 30 Dezember 2010 01:12 ] [ ID #2052241 ]

RE: [PHP] Regex for telephone numbers

I suggest you try javascript.



Richard L. Buskirk

-----Original Message-----
From: Ethan Rosenberg [mailto:ethros [at] earthlink.net]
Sent: Wednesday, December 29, 2010 7:12 PM
To: php-db-lists.php.net; php-general [at] lists.php.net
Subject: [PHP] Regex for telephone numbers

Dear List -

Thank you for all your help in the past.

Here is another one....

I would like to have a regex which would validate that a telephone
number is in the format xxx-xxx-xxxx.

Thanks.

Ethan

MySQL 5.1 PHP 5 Linux [Debian (sid)]



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
admin [ Do, 30 Dezember 2010 01:19 ] [ ID #2052242 ]

Re: Regex for telephone numbers

On 30/12/2010, at 1:12 PM, Ethan Rosenberg wrote:

> Dear List -
>
> Thank you for all your help in the past.
>
> Here is another one....
>
> I would like to have a regex which would validate that a telephone =
number is in the format xxx-xxx-xxxx.
>
> Thanks.
>
> Ethan
>
> MySQL 5.1 PHP 5 Linux [Debian (sid)]
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

\d{3}-\d{3}-\d{4}

Also, have a look at the phoneNumber method in the relevant Validate =
PEAR package: =
http://pear.php.net/packages.php?catpid=3D50&catname=3DValid ate

---
Simon Welsh
Admin of http://simon.geek.nz/

Who said Microsoft never created a bug-free program? The blue screen =
never, ever crashes!

http://www.thinkgeek.com/brain/gimme.cgi?wid=3D81d520e5e


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Simon Welsh [ Do, 30 Dezember 2010 01:23 ] [ ID #2052243 ]

RE: [PHP] Regex for telephone numbers

Also remove your stupid Email filter.
If you need a email filter, you should not be on this list or learn to setup
rules one.


Richard L. Buskirk


-----Original Message-----
From: Ethan Rosenberg [mailto:ethros [at] earthlink.net]
Sent: Wednesday, December 29, 2010 7:12 PM
To: php-db-lists.php.net; php-general [at] lists.php.net
Subject: [PHP] Regex for telephone numbers

Dear List -

Thank you for all your help in the past.

Here is another one....

I would like to have a regex which would validate that a telephone
number is in the format xxx-xxx-xxxx.

Thanks.

Ethan

MySQL 5.1 PHP 5 Linux [Debian (sid)]



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
admin [ Do, 30 Dezember 2010 01:21 ] [ ID #2052244 ]

Re: Regex for telephone numbers

On Dec 29, 2010, at 7:12 PM, Ethan Rosenberg <ethros [at] earthlink.net> wrote:

> Dear List -
>
> Thank you for all your help in the past.
>
> Here is another one....
>
> I would like to have a regex which would validate that a telephone number=
is in the format xxx-xxx-xxxx.
>
> Thanks.
>
> Ethan
>
> MySQL 5.1 PHP 5 Linux [Debian (sid)]
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

You can't, phone numbers are more complex then that. You could use \d{3}-\d{=
3}-\d{4} to match that basic pattern for all numbers though.

Regards,

-Josh
___________________________
http://joshuakehn.com
Sent from my iPod=

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Joshua Kehn [ Do, 30 Dezember 2010 01:27 ] [ ID #2052245 ]

Re: [PHP-DB] Re: [PHP] Regex for telephone numbers

Hi Ethan,
Could you do a string compare and check at certain characters for a
dash?
IE:
check the second character to see if it is a dash for 1-800...
if that is not a dash, check the fourth character for a dash, 469-9...
then the other places where dashes would be based on those two
characters.
You may have to investigate how international numbers would work and
adjust appropriately, but for the US, that should work.
Then just send an error message when it isn't like you want.
JAT

Karl


On Dec 29, 2010, at 6:27 PM, Josh Kehn wrote:

>
>
> On Dec 29, 2010, at 7:12 PM, Ethan Rosenberg <ethros [at] earthlink.net>
> wrote:
>
>> Dear List -
>>
>> Thank you for all your help in the past.
>>
>> Here is another one....
>>
>> I would like to have a regex which would validate that a
>> telephone number is in the format xxx-xxx-xxxx.
>>
>> Thanks.
>>
>> Ethan
>>
>> MySQL 5.1 PHP 5 Linux [Debian (sid)]
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>
> You can't, phone numbers are more complex then that. You could use
> \d{3}-\d{3}-\d{4} to match that basic pattern for all numbers though.
>
> Regards,
>
> -Josh
> ___________________________
> http://joshuakehn.com
> Sent from my iPod
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Karl DeSaulniers
Design Drumm
http://designdrumm.com


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Karl DeSaulniers [ Do, 30 Dezember 2010 01:46 ] [ ID #2052246 ]

Re: [PHP-DB] Re: [PHP] Regex for telephone numbers

You could also help them out a little with something like..

$phone = str_replace("(", "", $phone);
$phone = str_replace(")", "-", $phone);

HTH,

Karl


On Dec 29, 2010, at 6:27 PM, Josh Kehn wrote:

>
>
> On Dec 29, 2010, at 7:12 PM, Ethan Rosenberg <ethros [at] earthlink.net>
> wrote:
>
>> Dear List -
>>
>> Thank you for all your help in the past.
>>
>> Here is another one....
>>
>> I would like to have a regex which would validate that a
>> telephone number is in the format xxx-xxx-xxxx.
>>
>> Thanks.
>>
>> Ethan
>>
>> MySQL 5.1 PHP 5 Linux [Debian (sid)]
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>
> You can't, phone numbers are more complex then that. You could use
> \d{3}-\d{3}-\d{4} to match that basic pattern for all numbers though.
>
> Regards,
>
> -Josh
> ___________________________
> http://joshuakehn.com
> Sent from my iPod
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Karl DeSaulniers
Design Drumm
http://designdrumm.com


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Karl DeSaulniers [ Do, 30 Dezember 2010 01:49 ] [ ID #2052247 ]

Re: Regex for telephone numbers

On Wed, Dec 29, 2010 at 19:12, Ethan Rosenberg <ethros [at] earthlink.net> wrote=
:
> Dear List -
>
> Thank you for all your help in the past.
>
> Here is another one....
>
> I would like to have a regex =A0which would validate that a telephone num=
ber
> is in the format xxx-xxx-xxxx.

Congrats. People in Hell would like ice water. Now we all know
that everyone wants something. ;-P

Really, this isn't a PHP question, but rather one of regular
expressions. That said, something like this (untested) should work:

<?php

$numbers =3D array(
'123-456-7890',
'2-654-06547',
'sf34-asdf-',
'abc-def-ghij',
'555_555_5555',
'000-000-0000',
'8007396325',
'241-555-2091',
'800-555-0129',
'900-976-739',
'5352-342=3D452',
'200-200-2000',
);

foreach ($numbers as $n) {
echo $n.(validate_phone($n) ? ' is ' : ' is not ').'a valid
US/Canadian telephone number.'.PHP_EOL;
}


function validate_phone($number) {

if (preg_match('/^[2-9]{1,}[0-9]{2,}\-[2-9]{1,}[0-9]{2,}\-[0-9] {4,}=
$/',trim($number)))
{
return true;
}

return false;
}
?>


--
</Daniel P. Brown>
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
daniel.brown [ Do, 30 Dezember 2010 01:35 ] [ ID #2052248 ]

Re: [PHP] Regex for telephone numbers

At 07:27 PM 12/29/2010, Josh Kehn wrote:


>On Dec 29, 2010, at 7:12 PM, Ethan Rosenberg <ethros [at] earthlink.net> wrote:
>
> > Dear List -
> >
> > Thank you for all your help in the past.
> >
> > Here is another one....
> >
> > I would like to have a regex which would validate that a
> telephone number is in the format xxx-xxx-xxxx.
> >
> > Thanks.
> >
> > Ethan
> >
> > MySQL 5.1 PHP 5 Linux [Debian (sid)]
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
>You can't, phone numbers are more complex then that. You could use
>\d{3}-\d{3}-\d{4} to match that basic pattern for all numbers though.
>
>Regards,
>
>-Josh
>___________________________
>http://joshuakehn.com


++++++++++++

>Sent from my iPod
Josh -

I used use \d{3}-\d{3}-\d{4}.

It works beautifully!!

FYI [to all the list] -- I thank all for their input. I only needed
US phones, and I am forcing the user of the form to conform to
xxx-xxx-xxxx as the input format.

Ethan
++++++++++++



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ethan Rosenberg [ Do, 30 Dezember 2010 20:07 ] [ ID #2052282 ]

Re: [PHP-DB] Re: [PHP] Regex for telephone numbers

On Thu, Dec 30, 2010 at 14:07, Ethan Rosenberg <ethros [at] earthlink.net> wrote:
>
> Josh -
>
> I used use \d{3}-\d{3}-\d{4}.
>
> It works beautifully!!

Just keep in mind that invalid numbers will also pass that check,
such as 000-000-0000 or 123-456-6789. That's why my example was a bit
more involved.

--
</Daniel P. Brown>
Network Infrastructure Manager
Documentation, Webmaster Teams
http://www.php.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daniel Brown [ Fr, 31 Dezember 2010 00:36 ] [ ID #2052283 ]

Re: Regex for telephone numbers

On 12/29/2010 7:12 PM, Ethan Rosenberg wrote:
> Dear List -
>
> Thank you for all your help in the past.
>
> Here is another one....
>
> I would like to have a regex which would validate that a telephone number is
> in the format xxx-xxx-xxxx.
>
> Thanks.
>
> Ethan
>
> MySQL 5.1 PHP 5 Linux [Debian (sid)]
>

Regex is over-kill.

$phoneNum = preg_replace("%\D%", '', $phoneNum);//Remove everything except digits

$phoneNum = ltrim($phoneNum,'1');//Remove leading 1s

if(strlen($phoneValue) != 10)
{
throw new Exception("Phone number must be 10 digits, without leading a 1. Check
your entry carefull");
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Al [ Fr, 31 Dezember 2010 16:53 ] [ ID #2052286 ]

Re: Regex for telephone numbers

--000e0cd1d7505f16ef0498b7ce00
Content-Type: text/plain; charset=UTF-8

I guess, this will work fine

ereg('[0-9]{3}-[0-9]{3}-[0-9]{4}', $phone_number);

On Thu, Dec 30, 2010 at 2:12 AM, Ethan Rosenberg <ethros [at] earthlink.net>wrote:

> Dear List -
>
> Thank you for all your help in the past.
>
> Here is another one....
>
> I would like to have a regex which would validate that a telephone number
> is in the format xxx-xxx-xxxx.
>
> Thanks.
>
> Ethan
>
> MySQL 5.1 PHP 5 Linux [Debian (sid)]
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
with best regards,
Dmitriy.

--000e0cd1d7505f16ef0498b7ce00--
Dmitriy Ugnichenko [ Fr, 31 Dezember 2010 18:05 ] [ ID #2052289 ]

Re: [PHP] Regex for telephone numbers

On Fri, Dec 31, 2010 at 12:05, Dmitriy Ugnichenko
<mitya.ugnichenko [at] gmail.com> wrote:
> I guess, this will work fine
>
> ereg('[0-9]{3}-[0-9]{3}-[0-9]{4}', =A0$phone_number);

Not quite. Plus, all ereg* functions have been deprecated for
some time now.

--
</Daniel P. Brown>
Network Infrastructure Manager
Documentation, Webmaster Teams
http://www.php.net/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daniel Brown [ Fr, 31 Dezember 2010 18:20 ] [ ID #2052290 ]

Re: Regex for telephone numbers

On 12/29/2010 4:35 PM, Daniel P. Brown wrote:
> On Wed, Dec 29, 2010 at 19:12, Ethan Rosenberg <ethros [at] earthlink.net> wrote:
>> Dear List -
>>
>> Thank you for all your help in the past.
>>
>> Here is another one....
>>
>> I would like to have a regex which would validate that a telephone number
>> is in the format xxx-xxx-xxxx.
>
> Congrats. People in Hell would like ice water. Now we all know
> that everyone wants something. ;-P
>
> Really, this isn't a PHP question, but rather one of regular
> expressions. That said, something like this (untested) should work:
>
> <?php
>
> $numbers = array(
> '123-456-7890',
> '2-654-06547',
> 'sf34-asdf-',
> 'abc-def-ghij',
> '555_555_5555',
> '000-000-0000',
> '8007396325',
> '241-555-2091',
> '800-555-0129',
> '900-976-739',
> '5352-342=452',
> '200-200-2000',
> );
>
> foreach ($numbers as $n) {
> echo $n.(validate_phone($n) ? ' is ' : ' is not ').'a valid
> US/Canadian telephone number.'.PHP_EOL;
> }
>
>
> function validate_phone($number) {
>
> if (preg_match('/^[2-9]{1,}[0-9]{2,}\-[2-9]{1,}[0-9]{2,}\-[0-9] {4,}$/',trim($number)))
> {
> return true;
> }
>
> return false;
> }
> ?>
>
>

Actually...

Specified here [1] it says that the {1,} is the same as '+'. I think you should
drop the comma. If you don't this would be valid 844-2345-123456

^[2-9]{1,}[0-9]{2,}\-[2-9]{1,}[0-9]{2,}\-[0-9]{4,}$

should be

^[2-9]{1}[0-9]{2}\-[2-9]{1}[0-9]{2}\-[0-9]{4}$


1 http://us.php.net/manual/en/regexp.reference.repetition.php

Jim Lucas

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
List Manager [ Sa, 01 Januar 2011 01:09 ] [ ID #2052314 ]

Re: [PHP] Regex for telephone numbers

On Fri, Dec 31, 2010 at 19:09, Jim Lucas <lists [at] cmsws.com> wrote:
>
> Actually...
>
> Specified here [1] it says that the {1,} is the same as '+'. =A0I think y=
ou should
> drop the comma. =A0If you don't this would be valid 844-2345-123456
>
> ^[2-9]{1,}[0-9]{2,}\-[2-9]{1,}[0-9]{2,}\-[0-9]{4,}$
>
> should be
>
> ^[2-9]{1}[0-9]{2}\-[2-9]{1}[0-9]{2}\-[0-9]{4}$

Bah, you're absolutely correct. Force of habit with the commas.
I didn't even notice the sample test cases I put into that test array
didn't check for more than the number of digits per field, either.
Good catch, Jim, and Happy New Year.

--
</Daniel P. Brown>
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
daniel.brown [ Sa, 01 Januar 2011 01:36 ] [ ID #2052315 ]
PHP » gmane.comp.php.database » Regex for telephone numbers

Vorheriges Thema: Word Activity Application
Nächstes Thema: Re: [PHP] Re: Regex for telephone numbers