I wrote this validation method but am having issues with it.
function validate_password(pwd)
{
var score = 0;
// Min length is 8
if (pwd.length<8)
return false;
// Is lower present?
if (/[a-z]/g.test(pwd))
{
console.log('a-z test on "'+pwd+'":' + /[a-z]+/g.test(pwd));
score++;
}
// Is upper present?
if (/[A-Z]/g.test(pwd))
{
console.log('A-Z test on: "'+pwd+'":' + /[A-Z]+/g.test(pwd));
score++;
}
// Is digit present?
if (/\d/g.test(pwd))
{
console.log('digit test on: "'+pwd+'":' + /\d/g.test(pwd));
score++;
}
// Is special char present?
if (/\W/g.test(pwd))
{
console.log('spec char test on: "'+pwd+'":' + /\W/g.test(pwd));
score++;
}
if (score>=3)
return true;
else
return false;
}
This is what is written to the console:
>>> validate_password('aaasdfF#3s')
a-z test on "aaasdfF#3s":true
A-Z test on: "aaasdfF#3s":true
digit test on: "aaasdfF#3s":true
spec char test on: "aaasdfF#3s":true
true
>>> validate_password('aaasdfF#3s')
a-z test on "aaasdfF#3s":true
false
On the first try it seems to work as expected but when I call the method a 2nd time, it doesn't work as expected.
So, my question is why are there differences between the results from the first try and the 2nd try?
Thanks! :)