Javascript验证复杂密码

时间:2013-06-17 16:44:59

标签: javascript passwords password-confirmation password-checker

我正在创建一个创建帐户页面,我正在尝试验证他们创建的密码是否遵循正确的格式。 密码格式必须是: 最低: 15个字符 2 UPPER 2更低 2个数字 2特别

我已经搜索过,并且只能找到一个验证脚本来检查我正在使用的每个字符中的1个字符。我还使用一个函数来确认密码和confrim密码字段匹配密钥,以及一个单独的功能来提交相同的功能。验证它们在提交时是否匹配也不起作用。

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

<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>

我对javascript有点新意。详情将不胜感激。我希望有人能帮帮忙!谢谢!

我不是在寻找力量计或关键字验证。我只是想在提交时验证密码。

1 个答案:

答案 0 :(得分:9)

这应该这样做:

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;
}