1

Just when you think you've got a handle on regex; it all comes undone. Hoping to return a false check if anything other than alpha numeric and whitespace characters are found.

function checkName(fname)
{ 
  var rexp = new RegExp(/[^a-zA-Z0-9]\s/gim)
  if (!rexp.test(fname))
    {
      alert ("'" + fname + "'\nis okay")
    }
    else
    {
       alert ("'" + fname + "'\nis NOT okay")
    }
  return !rexp.test(fname)
}

I would hope that the above code would return for the following

  • "This is ok" - true
  • "This, is not ok" -false
  • "Nor is this ok!" -false
  • "Nor is \"this ok" - false
1
  • Please use either the RegExp constructor or regex literals, but not both. Commented Feb 19, 2013 at 13:13

4 Answers 4

2

Although much of the discussion is right, everything seems to be missing the point that you are inverting character classes and then inverting the results in your function. This is logically hard to read. You also do two tests on the regex for no good reason. Much cleaner would be something like this:

function checkName(fname) { 
    var result = /^[a-z0-9\s]+$/i.test(fname)

    if (result) {
        alert ("'" + fname + "'\nis okay")
    } else {
        alert ("'" + fname + "'\nis NOT okay")
    }
    return result;
}

Update: It looks like Jack's edits captured these points too. (Always a minute late and a nickel short...)

Sign up to request clarification or add additional context in comments.

Comments

1
[^a-zA-Z0-9]\s

Your regex requires the whitespace to be after the letters/numbers.

To fix it, move the \s inside the brackets.

You still have to do one more thing though. The regex will only match one of these characters. Add a + to match one or more.

Therefore, fixed regex:

[^a-zA-Z0-9\s]+

4 Comments

The expression was meant as the opposite, i.e. if there's at least one character in the "wrong" set, reject the value. Not sure what your answer will do, but I reckon it's not right :)
@Jack The OP said "other than alpha numeric and whitespace characters"
Yes, "other than" means no characters that are NOT alpha numeric :) I agree that the double negative isn't great, but even so, your expression should be anchored ... doing so will make it the same answer as Will.i.am though :)
@Jack Oh, that makes sense! I got confused by his wording :P Will update my answer now
1

A few things:

  1. /something/ is the short notation for new RegExp('something'); you shouldn't mix them up.

  2. You need to move the \s inside the character class; otherwise you match a character that's not alphanumeric followed by a space.

  3. I don't think you need all those modifiers:

    1. /m is only useful if you have anchors in your expression,
    2. /i can be used if you remove A-Z or a-z from the character class,
    3. /g is only useful for when you need to match multiple times, but in your case the first match is enough.

      var rexp = /[^a-zA-Z0-9\s]/;
      

The whole function can be written like this:

function checkName(fname)
{
    return !/[^a-zA-Z0-9\s]/.test(fname);
}

Instead of using double negatives, it would be better to say "only allow these characters":

function checkName(fname)
{
    return /^[a-zA-Z0-9\s]*$/.test(fname);
}

If you need to test for non-empty names as well, you should use /^[a-zA-Z0-9\s]+$/.

1 Comment

I wasn't the downvoter, but I like this answer much better after these edits!
0

Try:

function checkName(fname)
{ 
  var rexp = new RegExp(/^[a-z0-9\s]+$/i)
  if (!rexp.test(fname))
    {
      alert ("'" + fname + "'\nis okay")
    }
    else
    {
       alert ("'" + fname + "'\nis NOT okay")
    }
  return !rexp.test(fname)
}

1 Comment

You probably should explain that you reversed the expression and then anchored it; and in that case, you need to reverse the if condition.

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.