tags:

views:

96

answers:

4

How to validate phone number using php

+1  A: 

This link has a good example for your issue. You may need to change a bit to accommodate your country's phone format.

http://www.plasticbrain.net/resources/php-validate-email-address-and-phone-number/

clusterfu_k
+2  A: 

Phone numbers on mars have 15 digits and they start with ~. So check the input string if it starts with ~ and have length 15 with all trailing numerics.

this. __curious_geek
your missing the other planets..
Ben Rowe
In my country they don't. Plus there might be an international prefix. Or an extension. Depending on the country, digit groups get distanced with spaces, dots or dashes. Ergo: Validation is culture dependend and not quite as simple as you think.
christian studer
@christian: You forgot pause signals.
Alix Axel
+2  A: 

Since phone numbers must conform to a pattern, you can use regular expressions to match the entered phone number against the pattern you define in regexp.

php has both ereg and preg_match() functions. I'd suggest using preg_match() as there's more documentation for this style of regex.

An example

$phone = '000-0000-0000';

if(preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $phone)) {
  // $phone is valid
}
Ben Rowe
A: 

I depends heavily on which number formats you aim to support, and how strict you want to enforce number grouping, use of whitespace and other separators etc....

Take a look at this similar question to get some ideas.

Then there is E.164 which is a numbering standard recommendation from ITU-T

Ivar Bonsaksen