2011-03-11 64 views
2

我正在做一个非常简单的验证,检查两个字段是否相等。不幸的是,它似乎没有工作(当它返回true时返回false)。jQuery验证问题使用equalTo

这里是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> 

回答

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

它仍然无法正确验证,它似乎无视我输入的内容。 – AKor 2011-03-11 07:18:46

+0

@Sennheiser似乎你是正确的开始 – 2011-03-11 10:28:16

2

您可以像波纹管一样做 -

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

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

});