Зависит от того, насколько «достоверным» вы хотите его видеть.если все, что вы имеете в виду, это то, что он содержит ровно 10 цифр или 11 цифр с кодом страны ... тогда это, вероятно, довольно просто.
function telephoneCheck(str) {
var isValid = false;
//only allow numbers, dashes, dots parentheses, and spaces
if (/^[\d-()\s.]+$/ig.test(str)) {
//replace all non-numbers with an empty string
var justNumbers = str.replace(/\D/g, '');
var count = justNumbers.length;
if(count === 10 || (count === 11 && justNumbers[0] === "1") ){
isValid = true;
}
}
console.log(isValid, str);
return isValid;
}
telephoneCheck("555-555-5555"); //true
telephoneCheck("1-555-555-5555"); //true
telephoneCheck("(555)5555555"); //true
telephoneCheck("(555) 555-5555"); //true
telephoneCheck("555 555 5555"); //true
telephoneCheck("5555555555"); //true
telephoneCheck("1 555 555 5555") //true
telephoneCheck("2 555 555 5555") //false (wrong country code)
telephoneCheck("800-692-7753"); //true
telephoneCheck("800.692.7753"); //true
telephoneCheck("692-7753"); //false (no area code)
telephoneCheck(""); //false (empty)
telephoneCheck("4"); //false (not enough digits)
telephoneCheck("8oo-six427676;laskdjf"); //false (just crazy)
.as-console-wrapper{max-height:100% !important;}