Reputation: 2561
I'm very stumped here. I'm trying to take some server-side PHP and introduce some JavaScript that does the same thing - giving hints to the user as they type. I have the following, which is attempting to find all lowercase letters, uppercase letters, and numbers 0 or more times; it then deletes those characters, leaving behind a string containing everything else. I then take the length of this and compare it to a variable maxSymbols, which is 5.
I cannot get this to evaluate properly... what am I missing here?
else if(passwordValue.replace(/([a-zA-Z0-9])*/, '').length > maxSymbols){
// Check the maximum number of symbols in the password.
document.getElementById("passwordHint").innerHTML = "You've used too many symbols, " + maxSymbols + " is the maximum.";
document.getElementById("passwordHint").style.color = "red";
}
Upvotes: 0
Views: 63
Reputation: 25081
You are missing the g
modifier for your regular expression. Without the g
, you are only replacing the first match.
Try if(passwordValue.replace(/([a-zA-Z0-9])*/g, '').length > maxSymbols)
.
Hope this helps,
Pete
Upvotes: 1
Reputation: 78520
.replace(/([a-zA-Z0-9])*/, '')
should be
.replace(/[a-zA-Z0-9]/g, '')
and you should be good. :) What your are searching for in the first one is 0 or more of those characters together. What you need is that character class replaces globally (the g).
Upvotes: 0