This is another question about a previous question I had asked yesterday. I want user to be allowed to type US phone numbers in the following formats.
(800)-555-121
This function validate a phone number, return true if it validate and false if invalid. This function very simple i was wrote to.
/**
* @param $number
*
* @return bool
*/
function validatePhoneNumber($number) {
$formats = [
'###-###-####', '####-###-###',
'(###) ###-###', '####-####-####',
'##-###-####-####', '####-####', '###-###-###',
'#####-###-###', '##########', '#########',
'# ### #####', '#-### #####'
];
return in_array(
trim(preg_replace('/[0-9]/', '#', $number)),
$formats
);
}