使用equalTo的jQuery验证问题

时间:2011-03-11 06:44:40

标签: jquery validation

我正在做一个非常简单的验证,检查两个字段是否相等。不幸的是,它似乎不起作用(当它应该返回true时返回false)。

这是jQuery(#profSet是主要形式):

            //if either password field is filled, start trying to validate it
            if($("#chpw").val() != "" || $("#chpw2").val() != "")
            {
                $("#profSet").validate({
                    rules: {
                        chpw2: {
                            equalTo: "#chpw"
                        }
                    }
                });
                if($("#profSet").valid())
                {
                    $pv = 1;
                }
                else
                {
                    $pv = 0;
                }
            }

这是HTML:

    <tr>
        <td>Change Password</td><td><input type="password" id="chpw" name="chpw"/></td>
    </tr>
    <tr>
        <td>Confirm Password</td><td><input type="password" id="chpw2" name="chpw2"/><br><label for="chpw2" class="error" generated="true"></label></td>
  </tr>

2 个答案:

答案 0 :(得分:5)

我想类似

[更新]:对不起的假设答案

抱歉
<script type="text/javascript" src="scripts/jquery.js"></script>

<script type="text/javascript" src="scripts/jquery.validate.js"></script>

<script type="text/javascript">
$(function (){
    $('#form').validate({

        rules:{
            chpw:{
                required: true,
                equalTo: '#chpw2'
            }

        },

        messages:{
            chpw: 
                {
                required: "Password is required",
                equalTo: "Password must be equal"
                }

        }}
    )

})


</script>

<form id="form">
    <tr>
    <td>Change Password</td><td><input type="password" id="chpw" name="chpw"/></td>
    </tr>
    <tr>
    <td>Confirm Password</td><td><input type="password" id="chpw2" name="chpw2"/><br><label for="chpw2" class="error" generated="true"></label></td>
  </tr>
<input type="submit" name="submit" />  
</form>

答案 1 :(得分:2)

你可以像贝娄那样做 -

$("#formId").validate({
        rules: {

            chpw: {
                required: true,
                minlength: 6
            },
            chpw2: {
                required: true,
                minlength: 6,
                equalTo: "#chpw"
            }
        }
    });

});

相关问题