From 721ebf560f2e7647bdc5a0feac42aa2469e4e3ee Mon Sep 17 00:00:00 2001 From: CherryKitten Date: Thu, 13 Oct 2022 23:19:16 +0200 Subject: [PATCH] Add phone number validator certification project --- .../4-phoneNumber-validator.js | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 2-javascript-algorithms-datastructures/4-phoneNumber-validator.js diff --git a/2-javascript-algorithms-datastructures/4-phoneNumber-validator.js b/2-javascript-algorithms-datastructures/4-phoneNumber-validator.js new file mode 100644 index 0000000..0b293af --- /dev/null +++ b/2-javascript-algorithms-datastructures/4-phoneNumber-validator.js @@ -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") \ No newline at end of file