JS实现密码强度的判断

css部分代码

        span{display: block}

body部分代码

    <input type="text" id="pass">
    <input type="button" id="btn" value="验证密码强度">
    <span></span>

JavaScript部分代码

    var opass = document.getElementById("pass");
    var obtn = document.getElementById("btn");
    var ospan = document.querySelector("span");
    obtn.onclick = function(){
        var str = opass.value;
        if(str === ""){
            return;  
        }
        var a=0;
        var b=0;
        var c=0;
        for(var i=0;i<str.length;i++){
            if(str[i] >= 0 && str[i] <= 9){
                a = 1;
            }
            if(str[i] >= "a" && str[i] <= "z" || str[i] >= "A" && str[i] <= "Z"){
                b = 1;
            }
            if(!(str[i] >= 0 && str[i] <= 9 || str[i] >= "a" && str[i] <= "z" || str[i] >= "A" && str[i] <= "Z")){
                c = 1;
            }
        }
        switch(a+b+c){
            case 1:
                ospan.innerHTML = "简单";break;
            case 2:
                ospan.innerHTML = "一般";break;
            case 3:
                ospan.innerHTML = "困难";break;
        }
    }
    var num = Math.round(Math.random()*99999).toString();
    console.log(num);
    console.log(num.length);
    for(var i=0;i<num.length;i++){
        console.log(num[i]);
    }

实现效果
JS实现密码强度的判断
JS实现密码强度的判断
JS实现密码强度的判断

上一篇:DOM操作应用高级


下一篇:JS隐藏显示图片