So, the challenge is that we are trying to detect if a string matches a fixed phone number pattern, this is a simple string pattern.
The pattern is:
A slight variation of one of the other answers, but I believe the built-in character class should be slightly faster than a custom one:
return /^\d{3}-\d{3}-\d{4}$/.test(phoneNumber);
function whyNotBeSilly(pattern) {
return !(pattern.length !== 12 ||
(code = pattern.charCodeAt(0)) < 48 || code > 57 ||
(code = pattern.charCodeAt(1)) < 48 || code > 57 ||
(code = pattern.charCodeAt(2)) < 48 || code > 57 ||
(code = pattern.charCodeAt(4)) < 48 || code > 57 ||
(code = pattern.charCodeAt(5)) < 48 || code > 57 ||
(code = pattern.charCodeAt(6)) < 48 || code > 57 ||
(code = pattern.charCodeAt(8)) < 48 || code > 57 ||
(code = pattern.charCodeAt(9)) < 48 || code > 57 ||
(code = pattern.charCodeAt(10)) < 48 || code > 57 ||
(code = pattern.charCodeAt(11)) < 48 || code > 57 ||
pattern.charAt(3) != '-' || pattern.charAt(7) != '-');
}
Declaring a regex object pre-compiles it for any future use. Since you are looping through several test strings, it should perform better to instantiate the object outside the function first:
var rex = /^\d{3}-\d{3}-\d{4}$/;
Then the function would be:
function matchesPattern(pattern) {
return rex.test(pattern);
}
even faster than before:
function tecjam5(pattern) {
var c;
return !(pattern.length != 12 ||
!(((c=pattern.charCodeAt(2))>>3) == 6 || (c>>1) == 28) ||
!(((c=pattern.charCodeAt(4))>>3) == 6 || (c>>1) == 28) ||
!(((c=pattern.charCodeAt(11))>>1) == 28 || (c>>3) == 6) ||
!(((c=pattern.charCodeAt(0))>>3) == 6 || (c>>1) == 28) ||
!(((c=pattern.charCodeAt(1))>>3) == 6 || (c>>1) == 28) ||
!(((c=pattern.charCodeAt(5))>>3) == 6 || (c>>1) == 28) ||
!(((c=pattern.charCodeAt(6))>>3) == 6 || (c>>1) == 28) ||
!(((c=pattern.charCodeAt(8))>>3) == 6 || (c>>1) == 28) ||
!(((c=pattern.charCodeAt(9))>>3) == 6 || (c>>1) == 28) ||
!(((c=pattern.charCodeAt(10))>>1) == 28 || (c>>3) == 6) ||
pattern.charAt(3) != '-' || pattern.charAt(7) != '-');
}
(short: every number < 8 only need compared once)
very fast:
function tecjam3(pattern) {
if (pattern.length !== 12) {
return false;
}
code = pattern.charCodeAt(0);
if (code < 48 || code > 57) return false;
code = pattern.charCodeAt(1);
if (code < 48 || code > 57) return false;
code = pattern.charCodeAt(2);
if (code < 48 || code > 57) return false;
code = pattern.charCodeAt(4);
if (code < 48 || code > 57) return false;
code = pattern.charCodeAt(5);
if (code < 48 || code > 57) return false;
code = pattern.charCodeAt(6);
if (code < 48 || code > 57) return false;
code = pattern.charCodeAt(8);
if (code < 48 || code > 57) return false;
code = pattern.charCodeAt(9);
if (code < 48 || code > 57) return false;
code = pattern.charCodeAt(10);
if (code < 48 || code > 57) return false;
code = pattern.charCodeAt(11);
if (code < 48 || code > 57) return false;
if (pattern.charAt(3) != '-' || pattern.charAt(7) != '-') return false;
return true;
}
you can use regex for that:
if(/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/.test('123-456-7890'))
//ok
else
//not ok