2

matchArray becomes null for input like asklas@(((# How do I correct this behavior? I only want to allow characters and numbers..

function validateName(name) {
    debug(name);
    var namePat = /^(\[A-Za-z0-9]*)$/ ;
    var matchArray = name.match(namePat);
    if (!matchArray){
        debug ("Invalid name,", name );
        return false;
    } 
    return true;
}

2 Answers 2

4

There is one erroneous backslash in your regex. It should be

var namePat = /^[A-Za-z0-9]*$/;

(and you don't need the capturing parentheses, either).

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

Comments

1

Not sure what you want in this case... if you want a boolean output, use .test:

namePat.test(name)

... but null will work for your test (!matchArray) just fine.

It does seem like you have a typo in your regular expression - you'll want to get rid of the backslash before the opening bracket...

Comments

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.