0

I looked around and saw lots of regex examples but not one I'm looking for. I need to verify 10 digits, allow spaces and dashes but not parenthesis or other characters.

Here is what I'm currently using:

function validatePhone(phone) {
    var error = "";
    var stripped = phone.value.replace(/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/, '');

    if (phone.value == "") {
        document.getElementById('phone-error').innerHTML = "Please enter a phone number";
        phone.style.background = 'Yellow';
        var error = '6';
    } else if (isNaN(parseInt(stripped))) {
        var error = "5";
        document.getElementById('phone-error').innerHTML = "The phone number contains illegal characters.";
        phone.style.background = 'Yellow';
    } else if (stripped.length < 10) {
        var error = "6";
        document.getElementById('phone-error').innerHTML = "The phone number is too short.";
        phone.style.background = 'Yellow';
    } else {
        phone.style.background = 'White';
        document.getElementById('phone-error').innerHTML = '';
    }

Thanks!

justinae
  • 387
  • 1
  • 2
  • 14
  • It is significantly easier to just put a little note under the text box saying "please enter numbers only - no spaces or symbols" and then check you have 10 digits. – Niet the Dark Absol Feb 19 '14 at 01:16
  • 2
    Why validate like this? Phone numbers differ. Already, your 10-digit validation will fail my 11-digit mobile phone number. –  Feb 19 '14 at 01:16
  • 1
    Try this answer: http://stackoverflow.com/a/2386081/712526 It removes extra characters instead of disallowing them. (Hey, I *like* using parens when I type a phone number!) – jpaugh Feb 19 '14 at 01:22
  • All good comments. It's for an project that has specific requirements. – justinae Feb 19 '14 at 01:49
  • A good case for the Prussian Stance. http://chimera.labs.oreilly.com/books/1234000001527/ch03.html#vicunas-03-3 – DavidO Feb 19 '14 at 01:57
  • You should really be testing server-side. I run phone numbers through a regex that strips out all but numeric characters, then check that it is either 10 characters, or 11 preceded by a 1, if I'm only targeting the US and Canada. On the Javascript side, you could do the same thing. In this case I would also replace their field with only numeric, spaces, parentheses and hyphens, so that if they type things like letters, etc. they understand that is not going to be parsed. – John M. Feb 19 '14 at 02:09
  • why the down vote. it's a very legit question. if you don't like the objective then move on. – justinae Feb 19 '14 at 03:00
  • this is for an assignment, that is why the specific request. – justinae Feb 21 '14 at 04:54

2 Answers2

2

This is what I'd do:

function validatePhone(phone) {
    var error = "";
    var stripped = phone.value.replace(/\D+/, ''); // strips all non digit characters

    if (stripped == "") {
        document.getElementById('phone-error').innerHTML = "Please enter a phone number";
        phone.style.background = 'Yellow';
        error = '6';
    } else if (phone.value.match(/[^\d() .-]/)) {
        error = "5";
        document.getElementById('phone-error').innerHTML = "The phone number contains illegal characters.";
        phone.style.background = 'Yellow';
    } else if (stripped.length < 10) {
        error = "6";
        document.getElementById('phone-error').innerHTML = "The phone number is too short.";
        phone.style.background = 'Yellow';
    } else {
        phone.style.background = 'White';
        document.getElementById('phone-error').innerHTML = '';
    }
}       
JasperZelf
  • 2,731
  • 1
  • 22
  • 34
Toto
  • 89,455
  • 62
  • 89
  • 125
0

You could just replace \D with empty space... So that you only have digits left.

Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56