473,407 Members | 2,629 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,407 software developers and data experts.

preg_match method to validate phone numbers

uranuskid
Hey folks,

I'd like to validate phone number field. Any number in the formate xx-xxx-xxx(xx)
is supposed to pass the test (where x in brackets are optional). Here is what i used, but it doesn't work:
[PHP]if (!preg_match("/^[0-9]{2,2}[-]{1,1}[0-9]{3,3}[-]{1,1}[0-9]{3,5}$/", $phone)
{ echo "<p> Eroor message</p>";}

[/PHP]

I reckon it's just a matter of defining the optional fields, however, I don't know how. Any help would be most appreciated.

Cheers,
Frank
May 9 '07 #1
2 29111
pbmods
5,821 Expert 4TB
[PHP]if (!preg_match("/^[0-9]{2,2}[-]{1,1}[0-9]{3,3}[-]{1,1}[0-9]{3,5}$/", $phone)
{ echo "<p> Eroor message</p>";}
[/PHP]
You've got a pretty complicated regex there, but it doesn't have to be:

[PHP]
if(! preg_match('/^\d{2}-\d{3}-\d{3}(\d{2})?$/', $phone)) {
echo "<p> Error message</p>";
}
[/PHP]

Or if you wanted to get really compact...

[PHP]
if(! preg_match('/^\d{2}(-\d{3}){2}(\d{2})?$/', $phone)) {
echo "<p> Error message</p>";
}
[/PHP]

http://www.regular-expressions.info/reference.html
May 10 '07 #2
You've got a pretty complicated regex there, but it doesn't have to be:

[PHP]
if(! preg_match('/^\d{2}-\d{3}-\d{3}(\d{2})?$/', $phone)) {
echo "<p> Error message</p>";
}
[/PHP]

Or if you wanted to get really compact...

[PHP]
if(! preg_match('/^\d{2}(-\d{3}){2}(\d{2})?$/', $phone)) {
echo "<p> Error message</p>";
}
[/PHP]

http://www.regular-expressions.info/reference.html
Hey,

I tried your suggestions, however, wouldn't work for my purpose as the two last numbers are optional, so either one or two or none!

I got more into Regex and redefined my preg_match patter like this:
[PHP]
if(! preg_match('/^[0]{1}[3-9]{1}-\d{3}-\d{4}(\d{1,2})?$/',$phone))
{ echo "<p> Error message </p>"}
[/PHP]
I redefined the first two numbers as they should match NZ landlines!

Well, thanks anyway, especially for the link, it's a really good reference!!

Aloha,
Frank
May 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Andrew Banks | last post by:
In VS.NET, C#, I can validate agaist US Phone number, Zip, French phone number etc in the IDE... but nothing for the UK. Is there an online reference or add in for VS.NET that includes validation...
4
by: Earl | last post by:
I'm curious if there are others who have a better method of accepting/parsing phone numbers. I've used a couple of different techniques that are functional but I can't really say that I'm totally...
4
by: lilOlMe | last post by:
I'd love to be able to validate phone numbers in my software but my product is being used world wide. Not everyone's phone number is formatted like USA/Canada formats theirs. I've found a few...
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...
4
by: Blue Streak | last post by:
Hello, Folks! Does anyone know of a website that lists the local phone number formats for each country? TIA...
1
by: =?Utf-8?B?bW9uaWNhX2ZyYXppZXI=?= | last post by:
When I import contacts from excel using the import wizard, even after matching fields to field for some reason I can only get the first three to five from my list of 190 to import the phone numbers...
7
by: Propoflady | last post by:
My contacts can have as many as five or six phone numbers - is it possible to make a query that puts the name, and each phone number after it on the same line - for this reason I have two tables -...
6
by: lolodede | last post by:
i have three phone numbers fields and i only need to let the user enter one of them then the form submit but if te user didnt enter any number then the submit stopped thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.