参考文章: http://www.runoob.com/jquery/jquery-plugin-validate.html
1、使用的插件
<script src="${ctx}/theme/js/jquery.min.js?v=2.1.4"></script>
<script src="${ctx}/theme/js/plugins/validate/jquery.validate.min.js"></script>
<script src="${ctx}/theme/js/plugins/validate/messages_zh.min.js"></script>
<script src="${ctx}/theme/js/jquery.form.js"></script>
2、校验规则及方法
其中submitHandler是校验通过后进行的回调方法;通过ajaxSubmit方法进行表单的提交,并接受后台返回的JSON格式数据;
id对应为待校验和提交的form表单;
complete是完成后的回调函数,在里面进行显示后台返回的数据;
校验规则查看官方demo
function fromValidate(id) {
$("#"+id).validate({
rules: {
testname: "required",
/* username: {
required: true,
minlength: 2
}*/
},
messages: {
testname: "计划名称不能为空!",
},
submitHandler: function (form) { //通过之后回调
// form.submit();
$(form).ajaxSubmit({
dataType: "json",
beforeSend: function() {
},
uploadProgress: function(event, position, total, percentComplete) {
},
success: function() {
// alert(123);
},
complete: function(xhr) {
var data = xhr.responseText;
data = JSON.parse(data);
if (data.flag){
alert(“ok”);
} else{
alert(data.msg);
}
}
});
}
});
}