Add phone number validator certification project

This commit is contained in:
CherryKitten 2022-10-13 23:19:16 +02:00
parent ce8e046407
commit 721ebf560f
Signed by: sammy
GPG key ID: 0B696A86A853E955

View file

@ -0,0 +1,32 @@
function telephoneCheck(str) {
const regex = /((^((\(\d{3}\))|(\d{3})))(\s?-?(\d{3}))(\s?-?\d{4})$)|(^1(((\s|-)\d{3})|(\s?-?\(\d{3}\)))(\s?-?\d{3}\s?-?\d{4}))/
return regex.test(str);
}
// All of those should be true:
let testsTrue = [
telephoneCheck("555-555-5555"),
telephoneCheck("(555)555-5555"),
telephoneCheck("(555) 555-5555"),
telephoneCheck("555 555 5555"),
telephoneCheck("5555555555"),
telephoneCheck("1 555 555 5555"),
telephoneCheck("1 (555) 555-5555")
]
// All of those should be false:
let testsFalse = [
telephoneCheck("(555-555-5555"),
telephoneCheck("(555)5(55?)-5555"),
telephoneCheck("11 555-555-5555"),
telephoneCheck("10 (757) 622-7382"),
telephoneCheck("5555555"),
telephoneCheck("555-5555"),
telephoneCheck("55555555"),
telephoneCheck("27576227382")
]
console.log("True: " + testsTrue);
console.log("False: " + testsFalse);
console.log("bla")