$(function(){
var validator = $("#enterRegForm").validate({
debug:false, //调试模式取消submit的默认提交功能
//errorClass: "error",//默认为错误的样式类为:error
//validClass: "check",//验证成功后的样式,默认字符串valid
focusInvalid: true,//表单提交时,焦点会指向第一个没有通过验证的域
//focusCleanup:true;//焦点指向错误域时,隐藏错误信息,不可与focusInvalid一起使用!
onkeyup: true,
errorElement: "div",
submitHandler: function(form){ //表单提交句柄,为一回调函数,带一个参数:form
form.submit(); //提交表单
},
rules: {
"enterprise.enName": {
<wbr>required: true,<br><wbr>minlength: 6,<br><wbr>remote:{<br><wbr><wbr>url: "/nameServlet",<wbr><wbr><wbr><wbr>//后台处理程序<wbr><br><wbr><wbr>type: "get",<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//数据发送方式<br><wbr><wbr>dataType: "json",<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//接受数据格式<wbr><wbr><wbr><br><wbr><wbr>data: {<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//要传递的数据<br><wbr><wbr><wbr>enName: function() {<br><wbr><wbr><wbr>return $("#enName").val();<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr><br> }<br> },</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
"user.passWord":{
<wbr>required:true,<br><wbr>rangelength:[6,18]<br> },<br> passWordConf:{<br><wbr>required:true,<br><wbr>rangelength:[6,18],<br><wbr><span style="word-wrap:normal; word-break:normal; color:rgb(255,0,0)">equalTo:"#passWord" //此处的passWord 是<input id="passWord"> 一开始还以为是name的值呢,气死了<br></span>}</wbr></wbr></wbr></wbr></wbr>
},
messages: { //自定义验证消息
"enterprise.enName": {
<wbr>required: "请填写企业名称!",<br><wbr>minlength: $.format("至少要{0}个字符!"),<br><wbr>remote:$.format("该企业名称已存在!")<br><wbr><br> },<br> "user.passWord":{<br><wbr><wbr>required:"请填写确认密码!",<br><wbr><wbr>rangelength:$.format("密码要在{0}-{1}个字符之间!")<br><wbr>},<br> passWordConf:{<br><wbr><wbr>required:"请填写确认密码!",<br><wbr><wbr>rangelength:$.format("确认密码要在{0}-{1}个字符之间!"),<br><wbr><wbr>equalTo:"确认密码要和密码一致!"<br><wbr>},</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
errorPlacement: function(error, element) { //验证消息放置的地方
//error.appendTo( element.parent("td").next("td").children(".msg") );
<wbr>error.appendTo( element.parent(".field").next("div"));<br> },<br> highlight: function(element, errorClass) { //针对验证的表单设置高亮<br> $(element).addClass(errorClass);<br> },<br> success: function(div) {<br> div.addClass("valid");<br> }<br> });</wbr>
});
自定义方法;
新建一个js文件:$(document).ready(function(){
<wbr>// 字符最小长度验证(一个中文字符长度为2)<br><wbr><wbr><wbr>jQuery.validator.addMethod("stringMinLength", function(value, element, param) {<br><wbr><wbr><wbr><wbr>var length = value.length;<br><wbr><wbr><wbr><wbr>for ( var i = 0; i < value.length; i++) {<br><wbr><wbr><wbr><wbr><wbr>if (value.charCodeAt(i) > 127) {<br><wbr><wbr><wbr><wbr><wbr><wbr>length++;<br><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>return this.optional(element) || (length >= param);<br><wbr><wbr><wbr>}, $.validator.format("长度不能小于{0}!"));<br><wbr><wbr><br><wbr><wbr><wbr>// 字符最大长度验证(一个中文字符长度为2)<br><wbr><wbr><wbr>jQuery.validator.addMethod("stringMaxLength", function(value, element, param) {<br><wbr><wbr><wbr><wbr>var length = value.length;<br><wbr><wbr><wbr><wbr>for ( var i = 0; i < value.length; i++) {<br><wbr><wbr><wbr><wbr><wbr>if (value.charCodeAt(i) > 127) {<br><wbr><wbr><wbr><wbr><wbr><wbr>length++;<br><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>return this.optional(element) || (length <= param);<br><wbr><wbr><wbr>}, $.validator.format("长度不能大于{0}!"));</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr>// 字符验证<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr>jQuery.validator.addMethod("stringCheck", function(value, element) {<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr>return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr>}, "只能包括中文字、英文字母、数字和下划线");<wbr><wbr><br><wbr><wbr><br><wbr>// 中文字两个字节<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr>jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr>var length = value.length;<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr>for(var i = 0; i < value.length; i++){<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>if(value.charCodeAt(i) > 127){<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>length++;<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr>}<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr>return this.optional(element) || ( length >= param[0] && length <= param[1] );<wbr><wbr><wbr><wbr><wbr><wbr><br><wbr>}, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)");<wbr><wbr><br><wbr><wbr><wbr><br><wbr>// 字符验证<br><wbr><wbr><wbr>jQuery.validator.addMethod("string", function(value, element) {<br><wbr><wbr><wbr><wbr>return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);<br><wbr><wbr><wbr>}, "不允许包含特殊符号!");<br><wbr><wbr><wbr>// 必须以特定字符串开头验证<br><wbr><wbr><wbr>jQuery.validator.addMethod("begin", function(value, element, param) {<br><wbr><wbr><wbr><wbr>var begin = new RegExp("^" + param);<br><wbr><wbr><wbr><wbr>return this.optional(element) || (begin.test(value));<br><wbr><wbr><wbr>}, $.validator.format("必须以 {0} 开头!"));<br><wbr><wbr><wbr>// 验证两次输入值是否不相同<br><wbr><wbr><wbr>jQuery.validator.addMethod("notEqualTo", function(value, element, param) {<br><wbr><wbr><wbr><wbr>return value != $(param).val();<br><wbr><wbr><wbr>}, $.validator.format("两次输入不能相同!"));<br><wbr>// 验证值不允许与特定值等于<br><wbr><wbr><wbr>jQuery.validator.addMethod("notEqual", function(value, element, param) {<br><wbr><wbr><wbr><wbr>return value != param;<br><wbr><wbr><wbr>}, $.validator.format("输入值不允许为{0}!"));<br><wbr><wbr><br><wbr><wbr><wbr>// 验证值必须大于特定值(不能等于)<br><wbr><wbr><wbr>jQuery.validator.addMethod("gt", function(value, element, param) {<br><wbr><wbr><wbr><wbr>return value > param;<br><wbr><wbr><wbr>}, $.validator.format("输入值必须大于{0}!"));<br><wbr><wbr><br><wbr><wbr><wbr>// 验证值小数位数不能超过两位<br><wbr><wbr><wbr>jQuery.validator.addMethod("decimal", function(value, element) {<br><wbr><wbr><wbr><wbr>var decimal = /^-?\d+(\.\d{1,2})?$/;<br><wbr><wbr><wbr><wbr>return this.optional(element) || (decimal.test(value));<br><wbr><wbr><wbr>}, $.validator.format("小数位数不能超过两位!"));<br><wbr>//字母数字<br><wbr><wbr><wbr>jQuery.validator.addMethod("alnum", function(value, element) {<br><wbr><wbr><wbr>return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);<br><wbr><wbr><wbr>}, "只能包括英文字母和数字");<br><wbr>// 汉字<br><wbr><wbr><wbr>jQuery.validator.addMethod("chcharacter", function(value, element) {<br><wbr><wbr><wbr>var tel = /^[\u4e00-\u9fa5]+$/;<br><wbr><wbr><wbr>return this.optional(element) || (tel.test(value));<br><wbr><wbr><wbr>}, "请输入汉字");<br><wbr>// 身份证号码验证<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>