jQuery EasyUI 格式验证
程序员文章站
2022-06-17 13:54:33
...
var _formartDateStr = function (val) {
if (val < 10) {
val = "0" + val;
}
return val;
};
$.extend($.fn.validatebox.defaults.rules, {
//时间格式验证
DateValidate: {
validator: function (value, param) {
var result = false;
var _dateValid = /^((?:19|20|99)\d\d)-(0[1-9]|1[012]|[1-9])-(0[1-9]|[12][0-9]|3[01])$/;
result = value.match(_dateValid) ? true : false;
if (result) {
var _val = new Date(value);
var newDate = _val.getFullYear() + '-' + _formartDateStr(_val.getMonth() + 1) + '-' + _formartDateStr(_val.getDate());
if (newDate != value) {
result = false;
}
}
return result;
},
message: '时间格式应为,"年-月-日"'
},
//长时间格式验证
DateTimeValidate: {
validator: function (value, param) {
var result = false;
var _dateValid = /^((?:19|20|99)\d\d)-(0[1-9]|1[012]|[1-9])-(0[1-9]|[12][0-9]|3[01])\s(([0-1]\d)|(2[0-4])):[0-5]\d$/;
result = value.match(_dateValid) ? true : false;
if (result) {
var _val = new Date(value);
var newDate = _val.getFullYear() + '-' + _formartDateStr(_val.getMonth() + 1) + '-' + _formartDateStr(_val.getDate()) + " " + _formartDateStr(_val.getHours())+":"+_formartDateStr(_val.getMinutes());
if (newDate != value) {
result = false;
}
}
return result;
},
message: '时间格式应为,"年-月-日 时:分"'
},
//身份证号码格式验证
IDCardValidate: {
validator: function (value, param) {
//var _valid = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
var _valid = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
return _valid.test(value);
},
message: '身份证号码格式不正确'
},
//邮箱验证
EmailValidate: {
validator: function (value, param) {
var validStr = /^[a-zA-Z0-9_-][email protected][a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
return value.match(validStr) ? true : false;
},
message: '邮箱格式有误'
},
//IP地址验证
IPValidate: {
validator: function (value, param) {
var ipp = value.split(":");
if (ipp.length == 2) {
var ip = ipp[0].match(/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/);
var port = ipp[1].match(/^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]{1}|6553[0-5])$/);
if (ip == null) {
$.fn.validatebox.defaults.rules["IPValidate"].message = '地址格式有错'
return false;
}
if (port == null) {
$.fn.validatebox.defaults.rules["IPValidate"].message = '端口号格式有错'
return false;
}
return true;
}
$.fn.validatebox.defaults.rules["IPValidate"].message = '数据格式错误'
return false;
},
},
//验证手机号
PhoneNumValidate: {
validator: function (value, param) {
return /^0?(13|14|15|18|17)[0-9]{9}$/.test(value);
},
message: '请输入正确的手机号码'
},
//既验证手机号,又验证座机号
TelNumValidate: {
validator: function (value, param) {
return /(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\d3)|(\d{3}\-))?(1[34578]\d{9})$)/.test(value);
},
message: '请输入正确的电话号码'
},
//验证邮编 :邮政编码的验证(开头不能为0,共6位)
PostalNum: {
validator: function (value, param) {
return /^[1-9][0-9]{5}$/.test(value);
},
message: '请输入正确的邮政编码'
},
//只能输入字母和数字
NumberLetterValidate: {
validator: function (value, parm) {
var rs = value.match(/[^0-9^A-Z^a-z]/);
if (rs != null) {
$.fn.validatebox.defaults.rules["NumberLetterValidate"].message = parm.msg + "格式不正确,只包含数字和字母";
return false;
}
return true;
}
},
//只能输入数字
NumberValidate: {
validator: function (value, parm) {
var rs = value.match(/[^0-9]/);
if (rs != null) {
$.fn.validatebox.defaults.rules["OnlyNumberValidate"].message = parm.msg + "格式不正确,只包含数字";
return false;
}
return true;
}
},
//只能输入字母
LetterValidate: {
validator: function (value, parm) {
var rs = value.match(/[^A-Z^a-z^\s]/);
if (rs != null) {
$.fn.validatebox.defaults.rules["LetterValidate"].message = parm.msg + "格式不正确,只包含字母";
return false;
}
return true;
}
},
//url格式验证
URLValidate: {
validator: function (value, parm) {
var rs = value.match(/(http[s]?|ftp):\/\/[^\/\.]+?\..+\w$/i);
if (rs == null) {
$.fn.validatebox.defaults.rules["URLValidate"].message = parm.msg + "格式不正确,例:https://www.baidu.com/";
return false;
}
return true;
}
},
});