1

我正在创建一个创建帐户页面,并且正在尝试验证他们创建的密码是否遵循正确的格式。密码格式需要: 最少:15 个字符 2 大写 2 小写 2 数字 2 特殊

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

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

<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有点陌生。细节将不胜感激。我希望有人能帮帮忙!谢谢!

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

4

1 回答 1

11

这应该这样做:

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;
}
于 2013-06-17T17:04:12.493 回答