Wednesday 1 March 2017

regex - How to validate phone number using PHP?




How to validate phone number using php


Answer



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
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...