Regex for telephone numbers

Regex for telephone numbers

am 30.12.2010 01:12:28 von Ethan Rosenberg

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

RE: [PHP] Regex for telephone numbers

am 30.12.2010 01:19:06 von Administrator

I suggest you try javascript.



Richard L. Buskirk

-----Original Message-----
From: Ethan Rosenberg [mailto:ethros@earthlink.net]
Sent: Wednesday, December 29, 2010 7:12 PM
To: php-db-lists.php.net; php-general@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

RE: [PHP] Regex for telephone numbers

am 30.12.2010 01:21:06 von Administrator

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@earthlink.net]
Sent: Wednesday, December 29, 2010 7:12 PM
To: php-db-lists.php.net; php-general@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

Re: Regex for telephone numbers

am 30.12.2010 01:23:26 von Simon Welsh

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

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

\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

Re: Regex for telephone numbers

am 30.12.2010 01:27:05 von Joshua Kehn

On Dec 29, 2010, at 7:12 PM, Ethan Rosenberg wrote:

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

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

Re: Regex for telephone numbers

am 30.12.2010 01:35:50 von daniel.brown

On Wed, Dec 29, 2010 at 19:12, 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 =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:


$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;
}
?>


--=20

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

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

am 30.12.2010 01:46:36 von Karl DeSaulniers

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
> 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

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

am 30.12.2010 01:49:22 von Karl DeSaulniers

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
> 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

Re: [PHP] Regex for telephone numbers

am 30.12.2010 20:07:39 von Ethan Rosenberg

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


>On Dec 29, 2010, at 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)]
> >
> >
> > --
> > 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

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

am 31.12.2010 00:36:58 von Daniel Brown

On Thu, Dec 30, 2010 at 14:07, Ethan Rosenberg 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.

--

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

Re: Regex for telephone numbers

am 31.12.2010 16:53:24 von Al

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

Re: Regex for telephone numbers

am 31.12.2010 18:05:15 von Dmitriy Ugnichenko

--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 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--

Re: [PHP] Regex for telephone numbers

am 31.12.2010 18:20:01 von Daniel Brown

On Fri, Dec 31, 2010 at 12:05, Dmitriy Ugnichenko
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.

--=20

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

Re: Regex for telephone numbers

am 01.01.2011 01:09:51 von List Manager

On 12/29/2010 4:35 PM, Daniel P. Brown wrote:
> On Wed, Dec 29, 2010 at 19:12, 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.
>
> 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:
>
> >
> $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

Re: [PHP] Regex for telephone numbers

am 01.01.2011 01:36:18 von daniel.brown

On Fri, Dec 31, 2010 at 19:09, Jim Lucas 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.

--=20

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