Jquery 验证 equalTo 不适用于 bootstrap
Jquery Validation equalTo not working with bootstrap
我有一个包含密码字段的表单,我想通过比较两个密码字段的值来验证它:
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" name="pwd" required>
</div>
<div class="form-group">
<label for="repwd">Retype Password:</label>
<input type="password" class="form-control" id="repwd" name="repwd" required>
</div>
我的 jquery 验证规则是:
$("#registerform").validate({
rules: {
pwd: {
required: true,
equalTo: "#repwd"
},
repwd: {
required: true,
equalTo: "#pwd"
}
},
messages: {
repwd: {
equalTo: "Should be equal"
}
},
onkeyup: function(element){
this.element(element);
},
submitHandler: function(form) {
// do other things for a valid form
form.submit();
},
});
$('#pwd').on('change blur keyup', function() {
$('#pwd').valid();
});
所需规则已验证,但 equalTo 未验证。我该如何解决这个问题?
您可以简单地将 equalTo
添加到 pwd
因为这里 repwd
应该等于 pwd
而不是与您在 jquery 代码中所做的相反改变它应该工作。
演示代码 :
$("#registerform").validate({
rules: {
pwd: "required", //pwd required
repwd: {
required: true, //repwd required
equalTo: "#pwd" //should be equal to pwd
}
},
onkeyup: function(element) {
this.element(element);
},
submitHandler: function(form) {
// do other things for a valid form
form.submit();
},
});
$('#pwd').on('change blur keyup', function() {
$('#pwd').valid();
});
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
<form id="registerform">
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" name="pwd" required>
</div>
<div class="form-group">
<label for="repwd">Retype Password:</label>
<input type="password" class="form-control" id="repwd" name="repwd" required>
</div>
</form>
我有一个包含密码字段的表单,我想通过比较两个密码字段的值来验证它:
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" name="pwd" required>
</div>
<div class="form-group">
<label for="repwd">Retype Password:</label>
<input type="password" class="form-control" id="repwd" name="repwd" required>
</div>
我的 jquery 验证规则是:
$("#registerform").validate({
rules: {
pwd: {
required: true,
equalTo: "#repwd"
},
repwd: {
required: true,
equalTo: "#pwd"
}
},
messages: {
repwd: {
equalTo: "Should be equal"
}
},
onkeyup: function(element){
this.element(element);
},
submitHandler: function(form) {
// do other things for a valid form
form.submit();
},
});
$('#pwd').on('change blur keyup', function() {
$('#pwd').valid();
});
所需规则已验证,但 equalTo 未验证。我该如何解决这个问题?
您可以简单地将 equalTo
添加到 pwd
因为这里 repwd
应该等于 pwd
而不是与您在 jquery 代码中所做的相反改变它应该工作。
演示代码 :
$("#registerform").validate({
rules: {
pwd: "required", //pwd required
repwd: {
required: true, //repwd required
equalTo: "#pwd" //should be equal to pwd
}
},
onkeyup: function(element) {
this.element(element);
},
submitHandler: function(form) {
// do other things for a valid form
form.submit();
},
});
$('#pwd').on('change blur keyup', function() {
$('#pwd').valid();
});
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
<form id="registerform">
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" name="pwd" required>
</div>
<div class="form-group">
<label for="repwd">Retype Password:</label>
<input type="password" class="form-control" id="repwd" name="repwd" required>
</div>
</form>