-1
function phonenumberValidation(inputtext) {
    var phonenumber = /^(?([0-9]{3})\)?[-.]?([0-9]{3})[-.]?([0-9]{4})$/
    if (inputtext.value.match(phonenumber)); {
        alert("Valid Phone Number");
        return true;
    } else {
        alert("Not a valid Phone Number");
        return false;
    }
}

When I post this into jslint I get the following error from its validator:

error

Does anyone know what's wrong?

ruffin
  • 16,507
  • 9
  • 88
  • 138
Ashvin Ashoke
  • 43
  • 1
  • 4
  • Your regex syntax is probably not correct. – Phillip Chan Jun 02 '16 at 23:16
  • Try this `/^\(?([0-9]{3})\)?[-.]?([0-9]{3})[-.]?([0-9]{4})$/` – Diego Jun 02 '16 at 23:19
  • you did not explain what do you want to achieve from the regex. however it has a syntax error. use this: `/[0-9]{3}?[-.]?[0-9]{3}[-.]?[0-9]{4}$/` – Bamieh Jun 02 '16 at 23:23
  • Diego, I just tried your suggestion and got the exact same response from the validator. Thank you though – Ashvin Ashoke Jun 02 '16 at 23:23
  • 2
    Check your console for errors. But more basically, don't use regexp to check phone numbers. –  Jun 05 '16 at 05:12
  • Please give your question a meaningful title. –  Jun 05 '16 at 05:57
  • 1
    Note the `1.27` notation in the upper right of your screen shot. JSLint is helpfully telling you the **exact character location** of the problem it found. If you still can't figure it out at that point, then remove things one by one from the regexp until the problem goes away. The last thing you removed will be what is causing the problem. This is a basic debugging technique you should practice. Also, consider using a tool such as regex101.com. It will pinpoint exactly the location of errors in your regexp. In this case, it immediately reported the error `(? Unbalanced group`. –  Jun 05 '16 at 06:03
  • Once you've sorted that out, you can move on to your next syntax error, which is the extraneous semi-colon, which a quick glance at the console would show you--something like "Unexpected token else". –  Jun 05 '16 at 06:07
  • 1
    Also, for your sake and that of others, please format and indent your code. Your final `}` is not formatted as code, please fix that. –  Jun 05 '16 at 06:08

2 Answers2

0

I think you just need to escape those dashes in your regex. But then you've got a tiny bit more work to do before your code lints.

The below code lints, and the regex still checks out at regex101.com (hat tip to torazburo).

/*jslint browser:true */
/*global window */
function phonenumberValidation(inputtext) {
    "use strict";
    var phonenumber = /[0-9]{3}?[\-.]?[0-9]{3}[\-.]?[0-9]{4}$/;
    if (inputtext.value.match(phonenumber)) {
        window.alert("Valid Phone Number");
        return true;
    }

    // JSLint used to complain "Unnecessary 'else' after disruption.",
    // but that's apparently no longer a requirement.
    window.alert("Not a valid Phone Number");
    return false;
}

There's a secondary question in here, as @torazaburo also mentions: How do I check for a valid phone number in JavaScript, and it's got a number of answers on StackOverflow already.

I'm kind of partial to this one, though, to be clear, I've never used the library. It is Google's, though, and it's recently modified, so there's a pretty good chance it's doing better than whatever we'd come up with here quickly. Good luck.

Community
  • 1
  • 1
ruffin
  • 16,507
  • 9
  • 88
  • 138
-1

You actually have two errors. The first parenthesis in the regexp needs to be escaped, and there is an illegal semicolon on the if statement. The corrected function is:

function phonenumberValidation(inputtext) {
  var phonenumber = /^\(?([0-9]{3})\)?[-.]?([0-9]{3})[-.]?([0-9]{4})$/ 
  if (inputtext.value.match(phonenumber)) {
    alert("Valid Phone Number");
    return true;
  } else {
    alert("Not a valid Phone Number");
    return false;
  }
}
Michael Lorton
  • 43,060
  • 26
  • 103
  • 144