在单个页面上使用多表单验证 Clay Campbell 从 jsfiddle 和 Stackoverflow 借来并进行了重大修改 5-28-2013 这是使用 jQuery Validate 库,还使用了 Murach jQuery Book
$(document).ready(function () {
$.validator.setDefaults({
//debug: true, // blocks submit
errorElement: 'span', //default input error message container
errorClass: 'help-inline', // default input error message class
focusInvalid: false, // do not focus the last invalid input)
highlight: function (element) { // hightlight error inputs
$(element).closest('.control-group').addClass('error'); // set error class to the control group
},
unhighlight: function (element) { // revert the change done by hightlight
$(element).closest('.control-group').removeClass('error'); // set error class to the control group
},
}); // end ready function
// init validator obj and set the rules for registrationForm
$('#registrationForm').validate({
rules: {
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 6
},
verify: {
required: true,
equalTo: "#password"
},
firstName: {
required: true
},
lastName: {
required: true
},
address: {
required: true
},
city: {
required: true
},
state: {
required: true,
rangelength: [2, 2]
},
zip: {
required: true,
rangelength: [5, 10]
},
phone: {
required: true,
phoneUS: true
}
}
}); // end jQuery validation method call for registrationForm
// init validator obj and set the rules rules for memberForm
$('#memberForm').validate({
rules: {
emailMem: {
required: true,
email: true
},
passwordMem: {
required: true,
minlength: 6
}
}
}); // end jQuery validation method call for memberForm