A quick function used to validate and format US based phone numbers.. enter phone number into function, and either returns false if deemed to be invalid, or returns better formatted… idea for form validation checks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function VALIDATE_USPHONE($value)
{
$regex = '/(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})'
        .'(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})'
        .'[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?/'; 
 
    if ($Valid = preg_match($regex, $value, $matches ) ) {
    $formatted = "($matches[1]) $matches[2]-$matches[3]";
    if ($matches[4]) $formatted .= " x$matches[4]";
    return $formatted;
    } else {
    return false;
    }
 
}