首页 技术 正文
技术 2022年11月20日
0 收藏 859 点赞 4,792 浏览 1819 个字

$(document).ready(function() {
jQuery.validator.addMethod(“realName”, function(value, element) {
var tel = /^([\u4e00-\u9fa5]+|([a-zA-Z]+\s?)+)$/;
return this.optional(element) || (tel.test(value));
}, “请正确填写您的姓名”);
jQuery.validator.addMethod(“realWechatNum”, function(value, element) {
var tel = /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])\d{8}$/;
var rex = /^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/;
return this.optional(element) || (rex.test(value)) || (tel.test(value));
}, “微信号只能含有数字、字母、下划线、和减号,且长度在6-20位,开头为字母/手机号为11位数字”);
jQuery.validator.addMethod(“integer”, function(value, element) {
var tel = /^[1-9]*[1-9][0-9]*$/;
return this.optional(element) || (tel.test(value));
}, “请输入正确的员工编号”);
$(“#inputForm”).validate({
onfocusout: false,
rules: {
“staff.staffName”: {realName: true},
“staff.staffWorkNo”: {remote: “${ctx}/sys/user/checkStaffWorkNo?staffId=${sysUser.staffId}”},
“staff.staffNo”: {integer:true,maxlength:8},
wechatNum: {
realWechatNum:true,
remote: {
type: “post”,
url: “${ctx}/sys/user/checkWechatNum?userId=${sysUser.userId}”,
data: {
wechatNum: function(){return $(“#wechatNum”).val();}
}
}
}
},
messages: {
“staff.staffWorkNo”: {remote:”员工档案编码重复”},
wechatNum: {
realWechatNum: “微信号只能含有数字、字母、下划线、和减号,且长度在6-20位,开头为字母/手机号为11位数字”,
remote: “微信号/手机号已存在”
},
“staff.staffNo”: {
integer:”编号为非0开头正整数数字格式并且最多8位”,
maxlength:”编号为非0开头正整数数字格式并且最多8位”,
},
},
submitHandler: function(form){
$(‘#myModal’).modal({backdrop: ‘static’, keyboard: false});
loading(‘正在提交,请稍等…’);
form.submit();
},
errorContainer: “#messageBox”,
errorPlacement: function(error, element) {
$(“#messageBox”).text(“输入有误,请先更正。”);
if (element.is(“:checkbox”)||element.is(“:radio”)||element.parent().is(“.input-append”)){
error.appendTo(element.parent().parent());
} else {
error.insertAfter(element);
}
}
});
});

其中jQuery.validate的optional(element),用于表单控件的值不为空时才触发验证。
当element为空时this.optional(element)=true,用于在该控件为非必填项目时可以通过验证,及条件可以不填但是不能填错格式。

如果值为空时也要触发验证,移除optional(element)。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,905
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727