//自己的特殊字符
jQuery.validator.addMethod("teshuzifu", function(value, element) {
var tel = /[`~!@#$^&*()=|{}'\":;'%,_\\\\\\\\\\.<>/?~!@#¥……&*()——|{}]$/;
return this.optional(element) || (!tel.test(value));
}, "不能含有特殊字符");
// 身份证号码验证
jQuery.validator.addMethod("idcardno", function(value, element) {
return this.optional(element) || isIdCardNo(value);
}, "请正确输入身份证号码");
//字母数字
jQuery.validator.addMethod("alnum", function(value, element) {
return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
}, "只能包括英文字母和数字");
//邮政编码验证
jQuery.validator.addMethod("zipcode", function(value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, "请正确填写邮政编码");
//汉字
jQuery.validator.addMethod("chcharacter", function(value, element) {
var tel = /^[u4e00-u9fa5]+$/;
return this.optional(element) || (tel.test(value));
}, "请输入汉字");
//字符最小长度验证(一个中文字符长度为2)
jQuery.validator.addMethod("stringMinLength", function(value, element, param) {
var length = value.length;
for(var i = 0; i < value.length; i++) {
if(value.charCodeAt(i) > 127) {
length++;
}
}
return this.optional(element) || (length >= param);
}, $.validator.format("长度不能小于 { 0 }!"));
//字符最大长度验证(一个中文字符长度为2)
jQuery.validator.addMethod("stringMaxLength", function(value, element, param) {
var length = value.length;
for(var i = 0; i < value.length; i++) {
if(value.charCodeAt(i) > 127) {
length++;
}
}
return this.optional(element) || (length <= param);
}, $.validator.format("长度不能大于 { 0 }!"));
//字符验证
jQuery.validator.addMethod("string", function(value, element) {
return this.optional(element) || /^[u0391-uFFE5w]+$/.test(value);
}, "不允许包含特殊符号!");
//手机号码验证
jQuery.validator.addMethod("mobile", function(value, element) {
var length = value.length;
return this.optional(element) || (length == 11 && /^(((13[0-9]{1})|(15[0-9]{1}))+d{8})$/.test(value));
}, "手机号码格式错误!");
//电话号码验证
jQuery.validator.addMethod("phone", function(value, element) {
var tel = /^(d{3,4}-?)?d{7,9}$/g;
return this.optional(element) || (tel.test(value));
}, "电话号码格式错误!");
//必须以特定字符串开头验证
jQuery.validator.addMethod("begin", function(value, element, param) {
var begin = new RegExp(" ^ "+param);
return this.optional(element) || (begin.test(value));
}, $.validator.format("必须以 { 0 } 开头!"));
//验证两次输入值是否不相同
jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
return value != $(param).val();
}, $.validator.format("两次输入不能相同!"));
//验证值不允许与特定值等于
jQuery.validator.addMethod("notEqual", function(value, element, param) {
return value != param;
}, $.validator.format("输入值不允许为 { 0 }!"));
//验证值必须大于特定值(不能等于)
jQuery.validator.addMethod("gt", function(value, element, param) {
return value > param;
}, $.validator.format("输入值必须大于 { 0 }!"));
//小数点前最多9位,小数点后最多6位
jQuery.validator.addMethod("decimal", function (value, element) {
return this.optional(element) || /^([1-9]\d{0,8}|0)(\.\d{1,6})?$/.test(value);
}, "小数点前最多9位,小数点后最多6位^_^");
//结束时间不能小于开始时间
jQuery.validator.addMethod("laterTo", function (value, element, param) {
return $(param).val().split("-").join("") < $(element).val().split("-").join("");
}, "结束时间不能小于开始时间^_^");
<script src="<%=basePath %>static/js/plugins/validate/jquery.validate.min.js"></script>
<script src="<%=basePath %>static/js/plugins/validate/messages_zh.min.js"></script>
<script src="<%=basePath %>static/js/plugins/validate/additional-methods.js"></script>
<script type="text/javascript"> $.validator.setDefaults({ highlight: function (e) { $(e).closest(".form-group").removeClass("has-success").addClass("has-error"); }, success: function (e) { e.closest(".form-group").removeClass("has-error").addClass("has-success"); }, errorElement: "span", errorPlacement: function (e, r) { e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent()); }, errorClass: "help-block m-b-none", validClass: "help-block m-b-none" }), $().ready(function () { //$("#myform").validate(); var form = $( "#myform" ); var e = "<i class='fa fa-times-circle'></i> "; form.validate({ rules: { wenhao: { required: !0, teshuzifu: true } }, messages: { wenhao: {required: e + "必填", teshuzifu: e + "文号不能含有特殊字符"} }, submitHandler:function(form){ alert("提交事件!"); form.submit(); } }); //保存草稿箱 $("#savecaogao" ).click(function() { if(form.valid()){ //检查文件类型 if(checkFileType()==false){ return false; } myform.action="saveGw.action"; myform.submit(); } }); }); </script>