问题描述:

I'm using the following Regex to ensure that a password only has alphabetic and numeric characters.

if(! reFind("^[[:alnum:][:punct:]]", this.password)) {

this.addError(property="Password", message="Password must contain only letters, numbers, or punctuation marks.");

}

If I add a copyright character to the beginning of the word, the reFind blocks it; if I add it to the end of the word, it goes through. So abcd does not go through, while abcd does.

I want to make sure that I only allow alphanumeric and punctuation characters in my passwords.

网友答案:

Your regular expression just ensures that the first character is alphanumeric and/or punctuation. You want to ensure that every character is alphanumeric and/or punctuation. You can use either of these:

if(reFind("[^[:alnum:][:punct:]]", this.password))

if(! reFind("^[[:alnum:][:punct:]]*$", this.password))

(Note: these also differ from yours in that they allow a zero-length password. I figure that that's O.K., since you'll want to give a different error message in that case.)

网友答案:

Try this:

if(!reFind("^[[:alnum:][:punct:]]+$", this.password))
网友答案:

why can't you write something like:

^[0-9a-zA-Z]+$
相关阅读:
Top