Telephone Number Validator
//国家代码可以不提供,有则必须是1
//正则:数字1出现0次或1次 分隔符 3个数字 分隔符 3个数字 分隔符 4个数字
function telephoneCheck(str) {
let digitalOnly = str.replace(/\\D/g,"")
let len = digitalOnly.length
//无效-数字少于10个大于11个
if(len !== 10 && len !== 11)
return false
//无效-国家号码不是1
if(len === 11 && str[0]*1 !== 1)
return false
//有效-纯数字,位数也对(上面判断过了)
if(str.length === digitalOnly.length)
return true
//有效-地区部分有圆括号的格式
if(/\\D?[1]?\\D?\\(\\d{3}\\)\\D\\d{3}\\D\\d{4}/.test(str))
return true
//无效-地区部分圆括号不成对
if(/(\\(\\d{3})|(\\d{3}\\))/.test(str)===true && /\\(\\d{3}\\)/.test(str) === false)
return false
//普通过滤
return /\\D?[1]?\\D?\\d{3}\\D\\d{3}\\D\\d{4}/.test(str)
}
telephoneCheck("555-555-5555");
//看了其它解法后,学习、重构
/*
^(\\(1\\)|1)?
贴着开头
1 或者 (1)
可以不存在
[ -]?
间隔符
空格或短横杠
可以不存在
(\\(\\d{3}\\)|\\d{3})
三个数字,应该是比较大的区域性代码
可能被圆括号包裹
必须存在
[ -]?
间隔符
\\d{3}
三个数字,必须存在
[ -]?
间隔符
\\d{4}$
四个数字,必须存在
贴着尾部,以防其它不合法字符
*/
function telephoneCheck(str) {
return /^(\\(1\\)|1)?[ -]?(\\(\\d{3}\\)|\\d{3})[ -]?\\d{3}[ -]?\\d{4}$/.test(str)
}
telephoneCheck("555-555-5555");
测试数据:
telephoneCheck("555-555-5555") should return a boolean. telephoneCheck("1 555-555-5555") should return true. telephoneCheck("1 (555) 555-5555") should return true. telephoneCheck("5555555555") should return true. telephoneCheck("555-555-5555") should return true. telephoneCheck("(555)555-5555") should return true. telephoneCheck("1(555)555-5555") should return true. telephoneCheck("555-5555") should return false. telephoneCheck("5555555") should return false. telephoneCheck("1 555)555-5555") should return false. telephoneCheck("1 555 555 5555") should return true. telephoneCheck("1 456 789 4444") should return true. telephoneCheck("123**&!!asdf#") should return false. telephoneCheck("55555555") should return false. telephoneCheck("(6054756961)") should return false telephoneCheck("2 (757) 622-7382") should return false. telephoneCheck("0 (757) 622-7382") should return false. telephoneCheck("-1 (757) 622-7382") should return false telephoneCheck("2 757 622-7382") should return false. telephoneCheck("10 (757) 622-7382") should return false. telephoneCheck("27576227382") should return false. telephoneCheck("(275)76227382") should return false. telephoneCheck("2(757)6227382") should return false. telephoneCheck("2(757)622-7382") should return false. telephoneCheck("555)-555-5555") should return false. telephoneCheck("(555-555-5555") should return false. telephoneCheck("(555)5(55?)-5555") should return false.
别人家的孩子
function telephoneCheck(str) {
var regex = /^(1\\s?)?(\\(\\d{3}\\)|\\d{3})[\\s\\-]?\\d{3}[\\s\\-]?\\d{4}$/;
return regex.test(str);
}
telephoneCheck("555-555-5555");
//这里需要强调的一个策略就是,如果前面没有1,则前三个数是开头,^\\(\\d{3}\\) 或 ^\\d{3}
^
denotes the beginning of the string.(1\\s?)?
allows for “1” or "1 " if there is one.\\d{n}
checks for exactly n number of digits so \\d{3}
checks for three digits.x|y
checks for either x OR y so (\\(\\d{3}\\)|\\d{3})
checks for either three digits surrounded by parentheses, or three digits by themselves with no parentheses.[\\s\\-]?
checks for spaces or dashes between the groups of digits.$
denotes the end of the string. In this case the beginning ^
and end of the string $
are used in the regex to prevent it from matching any longer string that might contain a valid phone number (eg. “s 555 555 5555 3”).regex.test(str)
to test if the string adheres to the regular expression, it returns true
or false
.