复杂密码的 Javascript 验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17152677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:17:32  来源:igfitidea点击:

Javascript Validation for a Complex Password

javascriptpasswordspassword-confirmationpassword-checker

提问by jzamora1229

I'm creating a Create Account page and I'm trying to verify that the password they are creating follows the proper format. The password format needs to be: Minimum of: 15 characters 2 UPPER 2 lower 2 Numbers 2 Special

我正在创建一个创建帐户页面,我正在尝试验证他们创建的密码是否遵循正确的格式。密码格式必须为: 最少:15 个字符 2 UPPER 2 lower 2 Numbers 2 Special

I've searched and have only been able to find a validation script that checks for 1 of each character, which I am currently using. I'm also using a function to confirm the password and confrim password fields match on key up, and a separate function to do the same on submit. Validating that they match on submit is not working either.

我已经搜索过并且只能找到一个验证脚本,该脚本检查我目前正在使用的每个字符中的 1 个。我还使用一个函数来确认密码和确认密码字段在键上匹配,并使用一个单独的函数在提交时执行相同的操作。验证它们在提交时匹配也不起作用。

Here's what I have so far:

这是我到目前为止所拥有的:

<script>
var anUpperCase = /[A-Z]/;
var aLowerCase = /[a-z]/; 
var aNumber = /[0-9]/;
var aSpecial = /[!|@|#|$|%|^|&|*|(|)|-|_]/;

function testpasswd(form, ctrl, value)
{ 
    if (value.length < 15 || value.search(anUpperCase) == -1 ||  
        value.search (aLowerCase) == -1 || value.search (aNumber) == -1 || value.search (aSpecial) == -1)  
    { 
        document.getElementById("pw").innerHTML="Invalid Password"; 
    }  
    else 
    { 
        location.href = "submit.cfm"; 
    }  

}

function checkpasswds()   
{ 
  theForm = document.getElementById ( 'reg' ) ; 
  confm = document.getElementById ( 'confirm') ;
    if (theForm.passwd2.value != '' && theForm.passwd1.value != '')
    {
        if (theForm.passwd1.value != theForm.passwd2.value)
        { 
            confm.style.background = "url('images/wrong.png')" ; 
            confm.style.backgroundRepeat = "no-repeat" ;
            confm.style.backgroundPosition = "right" ;
        } 
        else
        { 
            confm.style.background = "url('images/correct.png')" ;
            confm.style.backgroundRepeat = "no-repeat" ; 
            confm.style.backgroundPosition = "right" ;
        } 
    } 
}

 function cnfmpasswd(form, ctrl, value)
{ 
    theForm = document.getElementById ( 'reg') ;
        if (theForm.passwd2.value != '' && theForm.passwd1.value != '')
    {
        if (theForm.passwd1.value != theForm.passwd2.value)
        { 
            return (false); 
        }  
        else 
        { 
            return (true); 
        }  
    }
}

function submitForm()
{
    document.forms['reg'].submit;
    testpasswd('reg','passwd1',document.getElementById('passwd1').value);
    cnfmpasswd('reg','passwd2',document.getElementById('passwd2').value);
}
</script>

<cfform action="submit.cfm" method="post" name="reg" id="reg" format="html">
<table width="947" border="0">
<tr>
<td width="180" align="right"><p style="color:#EB0000; font-size:14px" align="center" id="pw"></p></td>
<td width="118" align="right">
    Password:
</td>
<td width="635">
    <cfinput
    name="passwd1"
    title="Must contain at least 2 of each of the following: UPPERCASE, lowercase, numeric, and special characters"
    type="password"
    required="yes"
    onKeyUp="checkpasswds();"
    />
  (min 15 characters with 2 UPPER, 2 lower, 2 numeric, and 2 special)
</td>
</tr>
<tr>
<td id="confirm" align="right"></td>
<td align="right">
    Confirm Password:
</td>
<td>
    <cfinput
    name="passwd2"
    type="password"
    required="yes"
    onKeyUp="checkpasswds();"
    />
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>
    <cfinput name="submit"
    type="button"
    value="Submit"
    class="button"
    onClick="submitForm()"
    />
</td>
</tr>
</table>

I'm a little new with javascript. Details will be greatly appreciated. I hope someone can help! Thanks!

我对 javascript 有点陌生。细节将不胜感激。我希望有人能帮帮忙!谢谢!

I'm not looking for a strength meter or on keyup validation. I simply want to validate the password on submit.

我不是在寻找强度计或密钥验证。我只是想在提交时验证密码。

回答by Devin Lynch

This should do it:

这应该这样做:

var password = "TEstpass1aaaaaaa$$";
console.log(isOkPass(password));


function isOkPass(p){
    var anUpperCase = /[A-Z]/;
    var aLowerCase = /[a-z]/; 
    var aNumber = /[0-9]/;
    var aSpecial = /[!|@|#|$|%|^|&|*|(|)|-|_]/;
    var obj = {};
    obj.result = true;

    if(p.length < 15){
        obj.result=false;
        obj.error="Not long enough!"
        return obj;
    }

    var numUpper = 0;
    var numLower = 0;
    var numNums = 0;
    var numSpecials = 0;
    for(var i=0; i<p.length; i++){
        if(anUpperCase.test(p[i]))
            numUpper++;
        else if(aLowerCase.test(p[i]))
            numLower++;
        else if(aNumber.test(p[i]))
            numNums++;
        else if(aSpecial.test(p[i]))
            numSpecials++;
    }

    if(numUpper < 2 || numLower < 2 || numNums < 2 || numSpecials <2){
        obj.result=false;
        obj.error="Wrong Format!";
        return obj;
    }
    return obj;
}