How to validate number and capital letter in javascript

javascript, validation

Solution

"1".toUpperCase == "1" ! What do you say about that :) You could do your checking like this:

for(i=0;i<a.length;i++)
    {
        if('A' <= a[i] && a[i] <= 'Z') // check if you have an uppercase
            b++;
        if('a' <= a[i] && a[i] <= 'z') // check if you have a lowercase
            c++;
        if('0' <= a[i] && a[i] <= '9') // check if you have a numeric
            d++;
    }

Now if b, c, or d equals 0, there is a problem.

Problem

I want to validate password : - contain at least 1 number - contain at least 1 capital letter (uppercase) - contain at least 1 normal letter (lowercase) 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???

Original source