Thursday, March 7, 2013

Validating Telephone Numbers with PHP custom function

Validating Telephone Numbers with PHP (Server Side)

Here is the function that I came up with for validating telephone numbers with PHP:
function validatePhone($string) {
    $numbersOnly = ereg_replace("[^0-9]", "", $string);
    $numberOfDigits = strlen($numbersOnly);
    if ($numberOfDigits == 7 or $numberOfDigits == 10) {
        echo $numbersOnly;
    } else {
        echo 'Invalid Phone Number';
    }
}

1 comment: