-3

Near the end of my project and I require some assistant.I need a regex code to validate the phone number. If the phone number is not empty, validate that the phone number is in one of the following formats:

‘xxx–xxx-xxxx’ – 10 digits with two dashes where the first dash is between the third and fourth digits and the second dash is between the sixth and seventh digits.

‘xxxxxxxxxx’’ – all 10 digits, or,

‘(xxx)xxxxxxx’’ – 10 digits with the first three digits being enclosed by parentheses.

‘(xxx)–xxx-xxxx’ – 10 digits with the first three digits being enclosed by parentheses, and there is a dash between the sixth and seventh digits. Also it needs to provide an alert if the phone number is not in the required format. All formats must be accepted by the validation or else it fails.

My issue is that I made an attempt to do this with regular expression because I feel that this is easy for me. I’m not an experience programmer at all so write codes have never been my thing and my question is there a RegExpression that covers all the formats at once and still able to give me an alert if no number is selected and the user not using one of the required formats.

Here is my current code but it does not cover the four formats.

var phone = document.myForm.phone.value;
if (!/^\d{3}-\d{3}-\d{4}$/.test(document.myForm.phone.value)) {
alert("Please enter the correct phone format ");

This code only covers xxx-xxx-xxxx. Is it possible to cover all formats with this kind of code? Please it must be regex starting with “var phone” like I have in my code example or it tends to get messy.

Thanks in advance

Bigpat
  • 1
  • 1
  • 7

3 Answers3

1

Regex can get pretty tricky if you're pretty new to it. Anytime I need to test some regex/find help I use Regexr. Pretty neat little sandbox for testing regex.

As for your question I think this will work nicely.

var phone = [
    '555-555-5555',
    '(555)-555-5555',
    '(555)555-5555',
    '5555555555',
    '(555)5555555'
    ];

for ( var i = 0; i < phone.length; i++ ) {
    if ( !/(?:\d{1}\s)?\(?(\d{3})\)?-?\s?(\d{3})-?\s?(\d{4})/.test(phone[i]) ) {
        console.log(phone[i]+': invalid');
    }
}

Also I did use one of the community regex snippets from the site I linked above. Not taking credit for that.

Your regex itself is almost there. If you want to make something 'optional' use ? before whatever you want to make optional. In this case ( ) should be optional so \(?\d{3}\)? would make the ( ) optional but still force \d{3} to be required.

Wild Beard
  • 2,919
  • 1
  • 12
  • 24
  • [Why is using “for…in” with array iteration such a bad idea?](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea) – Xotic750 Jun 03 '15 at 15:11
  • I need an alert not the console.log. This does not work for me. – Bigpat Jun 03 '15 at 20:56
  • 1
    @Bigpat this is a test showing it checking several different phone number types in one snippet. If you want it to alert simply copy/paste the if statement and change the `console.log()` to `alert("whatever")`. – Wild Beard Jun 03 '15 at 20:58
0

Instead of making one complex/giant RegExp, why not test multiple simpler RegExp's?

var phones = [
        '0123456789',
        '012-345-6789',
        '(012)3456789'
    ],
    tests = [
        /^\d{3}-\d{3}-\d{4}$/,
        /^\d{10}$/
    ],
    pre = document.getElementById('out');

phones.forEach(function (phone) {
    pre.textContent += phone + ' : ';
    var pass = tests.some(function (test) {
        return test.test(phone.toString());
    });
    
    if (pass) {
        pre.textContent += 'pass';
    } else {
        pre.textContent += 'fail'
    }
    
    pre.textContent += '\n';
});
<pre id="out"></pre>

I've only included 2 rules in this example, but you can see it will be much easier to maintain, add/remove rules.

Xotic750
  • 22,914
  • 8
  • 57
  • 79
0

(?:^\d{3}-\d{3}-\d{4}$)|(?:^\(\d{3}\)\d{3}-\d{4}$)|(?:[0-9]{10})

This will cover the three examples you mentioned.

(If you build each example by itself then concatenate with a pipe (equivalent to 'or') you can add more...)

However if you are going to do a catch for ALL Valid Phone Number Formats, this will get quite lengthy. At that point i would suggest a different approach.

JStevens
  • 2,090
  • 1
  • 22
  • 26
  • This does not work with my code var phone = document.PizzaForm.phone.value; if (!(?:^\d{3}-\d{3}-\d{4}$)|(?:^\(\d{3}\)\d{3}-\d{4}$)|(?:[0-9]{10})/.test(document.PizzaForm.phone.value)) { alert("Please provide a correct phone number."); } – Bigpat Jun 03 '15 at 21:05