function isPattern(userInput) {
if (typeof userInput !== 'string' || userInput.length !== 12) {
return false;
}
for (let i = 0; i < userInput.length; i++) {
let c = userInput[i];
switch (i) {
case 0:
case 1:
case 2:
case 4:
case 5:
case 6:
case 8:
case 9:
case 10:
case 11:
if (c < 0 || c > 9) return false;
break;
case 3:
case 7:
if (c !== '-') return false;
break;
}
}
return true;
}
function isPattern2(userInput) {
return /^\d{3}-\d{3}-\d{4}$/.test(userInput);
}
isPattern('123-123-1234')
isPattern('1231231234')
isPattern('123-123-123a')
isPattern('12a-123-1234')
isPattern2('123-123-1234')
isPattern2('1231231234')
isPattern2('123-123-123a')
isPattern2('12a-123-1234')