I want to create a function that compares a password against some commonly idiotic ones, so that the user can’t pick one of these, but the function I have written so far, when put between script tags, causes no javascript to be recognized (by Firebug). I assume the array creation is at fault.
function unacceptable(pwd){
var unforgivable = [
/password/gi, /*g matches any occurance of sequence, i checks case insensitive*/
/12345678/g,
/8675309/g,
/[a-z]{8,}/gi,
/qwerty/gi,
/asdfg/gi,
/qazwsx/gi,
/zxcvb/gi,
/letmein/gi,
/trustno1/gi,
/omnicloud/gi,
/monkey/gi];
for (var i=0; i<unforgivable.length; i++)
if(pwd.match(unforgivable[i])) return true;
return false;
}
You don’t need the loop to test every word as you can put them all into one regular expression (separated by the
|character) and let the regex engine look for any of them all at once. You could do that like this:Working demo here: http://jsfiddle.net/jfriend00/cyVbC/
P.S. You don’t have to put all the words into an array. You could just predeclare the entire regex, but I thought putting them in the array like this made for more readable code that was easier to maintain.
It could also be this: