I want to validate password :
I used this code
function validate()
{
var a=document.getElementById("pass").value
var b=0
var c=0
var d=0;
for(i=0;i<a.length;i++)
{
if(a[i]==a[i].toUpperCase())
b++;
if(a[i]==a[i].toLowerCase())
c++;
if(!isNaN(a[i]))
d++;
}
if(a=="")
{
alert("Password must be filled")
}
else if(a)
{
alert("Total capital letter "+b)
alert("Total normal letter "+c)
alert("Total number"+d)
}
}
One thing that make me confuse is why if I input a number, it also count as uppercase letter???
Regular expressions are more suitable for this. Consider:
var containsDigits = /[0-9]/.test(password)
var containsUpper = /[A-Z]/.test(password)
var containsLower = /[a-z]/.test(password)
if (containsDigits && containsUpper && containsLower)
....ok
A more compact but less compatible option is to use a boolean aggregate over an array of regexes:
var rules = [/[0-9]/, /[A-Z]/, /[a-z]/]
var passwordOk = rules.every(function(r) { return r.test(password) });
Docs: test, every
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With