3

I need a regex to use with javascript/jquery that fits these rules...

  • it will include 10 digits
  • if there is a leading 1 or +1 it should be ignored
  • valid characters allowed in the field are... 0-9,(), and -

I found a regex at Snipplr (the first one), but its not working. First of all, I'm not even sure if that regex fits my rules. Secondly, its allowing inputs like &^%$$#%^adfafsd. I believe the error is in my code not the regex. For example, are there supposed to be quotes around the expression?

Here is the code that is supposed to be validating the phone field...

$('#phone').bind('blur', function() {
    var pattern = new RegExp("^(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})$");
    if(pattern.test($('#phone').val())){
        $("#phone").addClass("error");
        return false;
    }else{
        $("#phone").removeClass("error");
        return true;
    }
    return true;
})
1
  • are you looking for it to match any phone number? there's quite a large number of possibilities for phone number format when you include dashes and parenthesis, and then even more when you get into international numbers and extensions. Otherwise your code looks fine. The regexp works too. Commented Feb 1, 2012 at 19:16

2 Answers 2

4

When you're not using the literal form ( /[regex]/ ), you need to escape the regex string. Try this instead:

var regex = /^(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})$/;

if(regex.test($('#phone').val()){ ... }
Sign up to request clarification or add additional context in comments.

1 Comment

Sweet. That solved the syntax issue I was having with my code. I'm no longer getting errors when I inspect with Google Chrome. Now I just need a regex that works. Thanks!
2

if there is a leading 1 or +1 it should be ignored it will include 10 digits valid characters allowed in the field are... 0-9,(), and -

That could be matched with an expression like:

/^(?:\+?1)?[()-]*(?:\d[()-]*){10}$/

3 Comments

Oooooo, that was close. Its way closer than the other one I was trying. However, the "if there is a leading 1 or +1 it should be ignored" rule did not work. Example, I entered... (888)000-0000 and it did work, but when I added a 1 like this... 1(888)000-0000 it came back as not valid. The 1 should be ignored. It also said these were not valid, but they should be... +1(888)000-0000 and 18880000000 and +18880000000.
I got it... /^[\+?1]*[(]*(?:\d[)-]*){10}$/ ...lets the user put a 1 or +1 at the beginning and ignores it, allows for a ( following the 1 or +1 or to start and ignores it, allows for a ) somewhere in the string and ignores it, allows - and ignores it, only allows digits for the other characters and limits the string to 10 of them. It's not 100% perfect... ex: this is valid 62)34567890 ...but its really close and that's all I need. Thanks for the help.
@Kirkland, it was unclear what "it" referred to. Updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.