Wednesday 21 August 2019

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...