Tips for Validate US Telephone Numbers?

Tips for Validate US Telephone Numbers?
0

#1

i kinda dislike regex, know the very basic, can someone gime tips,
this is what i have https://www.freecodecamp.com/challenges/validate-us-telephone-numbers#?solution= function%20telephoneCheck(str)%20{ %20%20%2F%2F%20Good%20luck! %20%20 %20%20 %20%20 %20%20str%3Dstr.replace(%2F\s%2B%2Fg%2C’’).replace(%2F[^0-9]%2Fg%2C’’)%3B %20%20 %20%20if%20(str.length>9){ %20%20%20%20 %20%20%20%20return%20true%3B %20%20}else{ %20%20%20%20 %20%20%20%20%20%20%20return%20false%3B %20%20} %20%20 } telephoneCheck(“123**%26!!asdf%23”)%3B


#2

How about just stripping off everything that’s not a digit, then checking if it’s 10 digits, or 11 digits starting with a 1?


#3

@ShawnMilo That won’t work, some tests would fail.

I like to tackle complex regexes by composing them from smaller parts, like this:

// in strings: '\\' => '\'
var chars = '(\\w|[%-])+';
var scheme = '(https?:)?\\/\\/';
var host = '(' + chars + '\\.)+' + chars;
var path = '(\\/' + chars + ')*\\/?';
var simpleWebUrlRe = new RegExp('^' + scheme + host + path + '$');

simpleWebUrlRe.test('http://forum.freecodecamp.com/t/tips-for-validate-us-telephone-numbers/4071/2');

#4

I think some of the interactive regex tutorials are pretty good, such as http://tryregex.com/


#5

Try this

/^((1 )?\(\d{3}\) \d{3}-\d{4})|(1-)?(\d{3}-){2}\d{4}|(1\.)?(\d{3}\.){2}\d{4}|1?\d{10}$/

I took it from a book I bought 5 or 6 years ago called Regular Expression Recipes for Windows Developers

You may need to modify it to get exactly what you want, but I think it will be a good starting point.