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
2 29111
[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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 -...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: 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...
|
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...
|
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...
| |