js验证  两次密码是否输入一致  和  密码不能小于八位的方法如何写

解决方案 »

  1.   

    用正则表达式去匹配是否一致比较下两次输入就可以了,pasword1==password2
      

  2.   

    var zhengque="<img src='./image/zhengque.png' alt='正确' width='18' height='18' />";
    var cuowu="<img src='./image/cuowu.png' alt='错误' width='18' height='18' />";
    function checkValue(varInput) 
    {
           var tip = $(varInput.id+"_notice");
    //密码验证
    if(varInput.id=="password")
    {
    if(varInput.value==="")
    {
    tip.innerHTML=cuowu+"<font color='red'>密码不允许为空!</font>";
    }
    else
    {
    if(isPass(varInput.value))
    {
    tip.innerHTML=zhengque;
    }
    else
    {
    tip.innerHTML=cuowu+"<font color='red'>您填写的密码格式不正确!</font>";

    }
    }
    }

    //确认密码验证
    if(varInput.id=="password2")
    {
    if(varInput.value==="")
    {
    tip.innerHTML=cuowu+"<font color='red'>确认密码不允许为空!</font>";
    ckResult=0;
    }
    else
    {
    if(isPass(varInput.value))
    {
    if(varInput.value==$("password").value)
    {
    tip.innerHTML=zhengque;

    }
    else
    {
    tip.innerHTML=cuowu+"<font color='red'>您两次填写的密码不一致!</font>"; }
    }
    else
    {
    tip.innerHTML=cuowu+"<font color='red'>您填写的密码格式不正确!</font>";

    }
    }
    }html代码中table 部分
      <tr>
            <th>密码:</th>
            <td >
              <s:password  name="password" id="password" maxlength="20" onblur="checkValue(this);"  />
            <label id="password_notice"></label>
            <p>密码可使用英文、下划线、@符号、$符号、数字,总字符数在6 - 20之间。</p>
            </td>
            </tr>
            <tr>
            <th>确认密码:</th>
            <td >
            <s:password  name="password2" id="password2" maxlength="20" onblur="checkValue(this);"  />
            <label id="password2_notice"></label>
            <p>再次输入密码,确保密码无误。</p>
            </td>
            </tr>
      

  3.   

    上面JS漏掉了正则表达式验证,下面补上,密码长度为8到20位
    function isPass(str)
    {
    str=trim(str);
    var vReg = /^[\w@$]{8,20}$/;
    return vReg.test(str);
    }
      

  4.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript">
    /**
                 * author: develop_design_level
                 * date: 2009-10-30
                 * @param {Object} id
                 */
                function $(id){
                    if (document.getElementById) {
                        return document.getElementById(id);
                    }
                    else {
                        return document.all.id;
                    }
                }
    function checkPwd(){
    var oldPwd = $('oldPwdId').value;
    var newPwd = $('newPwdId').value;
    if(oldPwd.length < 8){
    alert('密码1的程度不够8位,请重新输入!');
    $('oldPwdId').focus();
    $('oldPwdId').select();
    return;
    }
    if(newPwd.length < 8){
    alert('密码2的程度不够8位,请重新输入!');
    $('oldPwdId').value = '';
    $('newPwdId').value = '';
    $('oldPwdId').focus();
    return;
    }
    if(oldPwd != newPwd){
    alert('输入的两次密码不一致,请重新输入!');
    $('oldPwdId').value = '';
    $('newPwdId').value = '';
    $('oldPwdId').focus();
    }else{
    alert('---- 密码一致,恭喜!----');
    }
    }
    window.onload = function(){
    $('btnId').onclick = checkPwd;
    };
    </script>
    </head>
    <body>
    <form>
    密码1:<input type="password" id="oldPwdId" /><br/>
    密码2:<input type="password" id="newPwdId" /><br/>
    <input type="button" id="btnId" value=" 提交 "/>
    </form>
    </body>
    </html>