Saturday, 23 September 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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...