I want to add contact form phone number field. Some countries have 12 digit and some countries have 11 digit numbers. So i want to required this field min. digit 10 and max 12. Herewith i mentioned validation code.
/* Phone */
function validatePhone($phone) {
$error_text = "Phone format: (xxx) xxxx-xxxxx";
$phone_template = "/^\([0-9]{3}\) [0-9]{4}-[0-9]{5}$/";
return (preg_match($phone_template, $phone) !== 1) ? $error_text : "valid";
}
You could try this,
function validatePhone($phone) {
$error_text = "Phone format: (xxx)-xxxx-xxxxx";
$remove = array("(", ")", " ", "-");
if (preg_match('/^\d{11,12}$/', str_replace($remove, '', $phone))) {
// valid
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments