yj-t
10/30/2015 - 7:12 AM

FormIt validator for phone number based on: https://regex101.com/r/xX3hO8/1

FormIt validator for phone number based on: https://regex101.com/r/xX3hO8/1

<?php
$success = false;
if (!empty($value)) {
    $re = "/^((((\+[\d\-.]{1,5})?[ \-.]?\d{3})|(\+[\d\-.]{1,5})?[ \-.]?\((\d{3}\)))?[ \-.]?\d{3}[ \-.]?\d{4}\s?(e?x?t?\.?\s?\d{1,7})?)?$/i";
    if (preg_match($re, $value) === 1) $success = true;
}
if (!$success) {
    $validator->addError($key, 'Please enter a valid phone number.');
    return false;
} else {
    return true;
}