jquery-validae
$(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));
}, "請(qǐng)正確填寫(xiě)您的姓名");
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));
}, "微信號(hào)只能含有數(shù)字、字母、下劃線、和減號(hào),且長(zhǎng)度在6-20位,開(kāi)頭為字母/手機(jī)號(hào)為11位數(shù)字");
jQuery.validator.addMethod("integer", function(value, element) {
var tel = /^[1-9]*[1-9][0-9]*$/;
return this.optional(element) || (tel.test(value));
}, "請(qǐng)輸入正確的員工編號(hào)");
$("#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:"員工檔案編碼重復(fù)"},
wechatNum: {
realWechatNum: "微信號(hào)只能含有數(shù)字、字母、下劃線、和減號(hào),且長(zhǎng)度在6-20位,開(kāi)頭為字母/手機(jī)號(hào)為11位數(shù)字",
remote: "微信號(hào)/手機(jī)號(hào)已存在"
},
"staff.staffNo": {
integer:"編號(hào)為非0開(kāi)頭正整數(shù)數(shù)字格式并且最多8位",
maxlength:"編號(hào)為非0開(kāi)頭正整數(shù)數(shù)字格式并且最多8位",
},
},
submitHandler: function(form){
$('#myModal').modal({backdrop: 'static', keyboard: false});
loading('正在提交,請(qǐng)稍等...');
form.submit();
},
errorContainer: "#messageBox",
errorPlacement: function(error, element) {
$("#messageBox").text("輸入有誤,請(qǐng)先更正。");
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),用于表單控件的值不為空時(shí)才觸發(fā)驗(yàn)證。
當(dāng)element為空時(shí)this.optional(element)=true,用于在該控件為非必填項(xiàng)目時(shí)可以通過(guò)驗(yàn)證,及條件可以不填但是不能填錯(cuò)格式。
如果值為空時(shí)也要觸發(fā)驗(yàn)證,移除optional(element)。
轉(zhuǎn)載于:https://www.cnblogs.com/h-wei/p/10149790.html
總結(jié)
以上是生活随笔為你收集整理的jquery-validae的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 腾讯面经来了
- 下一篇: 那个 CEO 写下 70 万行代码的公司