473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

regexp phone # check

I'm working on validating US phone numbers. I have a nice expression that
Regex Coach likes, but causes PHP to reject everything I send. Are there
any glaring differences? I can't figure out what's wrong. Another little
email check works fine, using the code out of Wrox' Beginning PHP.

PhoneCheck.php
<?php
function PhoneCheck($number)
{
return ereg("^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$", $number,
$scrap);
}
?>

My test cases (which passed regex coach, but failed my php form):
123 456 7890
(123) 456-7890
123.456.7890
123-456-7890

Thanks,
-G
Jul 17 '05 #1
8 11017
Greg Bryant wrote:
I'm working on validating US phone numbers. I have a nice expression that
Regex Coach likes, but causes PHP to reject everything I send. Are there
any glaring differences? I can't figure out what's wrong. Another little
email check works fine, using the code out of Wrox' Beginning PHP.


try preg_match() instead of ereg()

and don't forget the regex delimiters
<?php
function PhoneCheck($number)
{
return preg_match("/^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$/", $number, $scrap);
}
?>

--
..sig
Jul 17 '05 #2
Pedro Graca <he****@hotpop.com> wrote in
news:bo*************@ID-203069.news.uni-berlin.de:
try preg_match() instead of ereg()

and don't forget the regex delimiters
<?php
function PhoneCheck($number)
{
return preg_match("/^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$/",
$number, $scrap);
}
?>


Thanks!
Jul 17 '05 #3
On Sun, 09 Nov 2003 01:34:18 GMT, Greg Bryant <br**********@yahoo.com> wrote:
return ereg("^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$", $number,
$scrap);


(Someone else has already pointed out to use preg_match instead of ereg - \d
doesn't work in ereg)

Put regexes inside single quotes instead of double quotes - it saves you some
escaping.

Inside double quotes, "\." is the same as ".", so it gets to the regex as a
"." - match any character.

Inside single quotes, '\.' stays as '\.', so gets to the regex as '\.' - match
a literal '.' character.

You don't need to escape . inside a character class anyway, so in this case it
didnt make a difference.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #4
Andy Hassall, obviously a huge fan of Grandmaster Flash, wrote:

Inside double quotes, "\." is the same as ".", so it gets to the regex as a
"." - match any character.


Hmm, not in my experience...

fnord@demogorgon:~$ cat preg.php
<?php
if(preg_match("'^\.$'", "."))
echo "\"'^\\.$'\" matches \".\".\n";
if(preg_match("'^\.$'", "x"))
echo "\"'^\\.$'\" matches \"x\".\n";
?>
fnord@demogorgon:~$ php4 -q preg.php
"'^\.$'" matches ".".
fnord@demogorgon:~$

/joe
--
In the Rich building, the decompiler is pernicious. In the beer fridge, the
processor is fanatic... The fantastic anus is 2x-ice-cold.
Jul 17 '05 #5
Andy Hassall wrote:
Put regexes inside single quotes instead of double quotes - it saves you some
escaping.
Well, it certainly saves parsing the string for variables. But
perhaps that may be desired from time to time.
Inside double quotes, "\." is the same as ".", so it gets to the regex as a
"." - match any character.
I don't think so. A slash followed by a period is understood to
mean a literal period in a regular expression, whether it's within
single- or double-quotes, or within a character class.

$string = 'abc';
echo preg_match("`ab(?=\.)`",$string) ? 'Match' : 'No match';
echo preg_match('`ab(?=\.)`',$string) ? 'Match' : 'No match';

Moreover, in any single- or double-quoted string, a period
following a backslash is treated literally -- that is, "\.".

http://www.php.net/manual/en/language.types.string.php
You don't need to escape . inside a character class anyway, so in this case it
didnt make a difference.


Right.

I'm not familiar with the format(s) of US phone numbers, but, from
the test cases provided, I suspect the hyphen must be escaped. If
it isn't, the characters ")", "*", "+", ",", "-", ".", " ", and
"]" will be permitted by the second character class (I.e., those
of the ASCII repertoire between RIGHT PARENTHESIS and FULL STOP
inclusive, and the RIGHT SQUARE BRACKET and SPACE).

Hmm. Is "(999]...]) ]---]999 9999" really a proper national
phone number?

--
Jock
Jul 17 '05 #6
On Sun, 9 Nov 2003 16:03:10 +0000 (UTC), Disco Plumber <sc**@moralminority.org>
wrote:
Andy Hassall, obviously a huge fan of Grandmaster Flash, wrote:

Inside double quotes, "\." is the same as ".", so it gets to the regex as a
"." - match any character.


Hmm, not in my experience...

fnord@demogorgon:~$ cat preg.php
<?php
if(preg_match("'^\.$'", "."))
echo "\"'^\\.$'\" matches \".\".\n";
if(preg_match("'^\.$'", "x"))
echo "\"'^\\.$'\" matches \"x\".\n";
?>
fnord@demogorgon:~$ php4 -q preg.php
"'^\.$'" matches ".".
fnord@demogorgon:~$


Whoops - my mistake.

So ignore all my post except the bit about not needing to escape . in a
character class.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #7
Andy Hassall (34.365% quality rating):

Whoops - my mistake.
So ignore all my post except the bit about not needing to escape . in a
character class.


I actually mentioned it to my friend, and he agreed with what you said,
but then I showed him the code. He said it might be a version difference.
For reference, the previous post was run with PHP 4.1.2.

/joe
--
A Playstation is long. The case from Freebeer will go to El Myr!
Jul 17 '05 #8
Andy Hassall (97.635% quality rating):

So ignore all my post except the bit about not needing to escape . in a
character class.


And you know, I actually didn't realize that, but it makes perfect
sense. Having a match-all character in a character class would be silly.

/joe
--
In the Atlanta Diner, Nick Ralabate barely felches the Student Center for
the anus, and then interestingly memorizes the configuration of Jon
Beckham's T1? In 'Narz!, Pizza says stupid shit about Q, and then triply,
cleverly, doubly jacks into David Wada's sig generator f... [tape runs out]
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
10
by: Jeff Sandler | last post by:
I have a page that accepts input from many textboxes. Many of the textboxes are intended to accept dates and times, thus, I expect only digits to be entered. I originally tested using parseInt...
3
by: zzzxtreme | last post by:
hello i have this function to check date (not mine) function (s_date) { // check format if (!re_dt.test(s_date)) return false; // check allowed ranges if (RegExp.$1 > 31 || RegExp.$2 > 12)...
26
by: Matt Kruse | last post by:
Are there any current browsers that have Javascript support, but not RegExp support? For example, cell phone browsers, blackberrys, or other "minimal" browsers? I know that someone using Netscape...
6
by: Christoph | last post by:
I'm trying to set up client side validation for a textarea form element to ensure that the data entered does not exceed 200 characters. I'm using the following code but it doesn't seem to be...
14
by: Ørjan Langbakk | last post by:
I have a form where the user has the possibility to enclose his name. email, address and phonenumber. I want to be able to check if some of the fields are filled - at least one. This is so that...
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
5
by: Abhishek | last post by:
Hi this is my another validator in javascript to validate the Phone Number :-) <script language='javascript'> function funcCheckPhoneNumber(ctrtxtMobile,e){ if(window.event){ var strkeyIE =...
4
by: luke noob | last post by:
This is my HTML... <head> <script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script> <script type="text/javascript" src="js/script.js"></script> </head> <body>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.