页面引入jquery.validate.min.js
要验证的form,必须是form表单,提交不提交的无所谓
js中如下:
//验证方法function valContent(){ return $("#content").validate({ errorLabelContainer: $("#content div.error"), rules: { fieldName: { //fieldName 是页面的name属性 required: true }, orderNumber:{ number: true, digits: true, min :0 } }, messages: { fieldName: { required: "请填写字段名称" }, orderNumber:{ number: "请输入合法的排序数字", digits: "排序只能输入整数", min :$.validator.format("不能小于0") } } }).form();}
发送的ajax请求如下:
//新增数据字典 $("#insertField").click(function(){ var fieldType = $("#fieldType").val(); var fieldName = $("#fieldName").val(); var orderNumber = $("#orderNumber").val(); var isDisable = $("input[type='radio']:checked").val(); var remark = $("#remark").val(); if(valContent()){ $.post(ctx+"/field/insert",{fieldType:fieldType,fieldName:fieldName,orderNumber:orderNumber,isDisable:isDisable,remark:remark},function(msg){ if(msg.success){ $("#myModal").modal('toggle'); loadSignIn(); } }); } });