$.extend($.fn.validatebox.defaults.rules, {
CHS: {
validator: function (value, param) {
return /^[\u0391-\uFFE5]+$/.test(value);
},
message: '请输入汉字'
},
ZIP: {
validator: function (value, param) {
return /^[1-9]\d{5}$/.test(value);
},
message: '邮政编码不存在'
},
email: {
validator: function (value, param) {
return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(value);
},
message: '邮箱格式有误'
},
QQ: {
validator: function (value, param) {
return /^[1-9][0-9]{4,9}$/.test(value);
},
message: 'QQ号码不正确'
},
mobile: {
validator: function (value, param) {
return /^(0?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8})$/.test(value)||/^(0?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}\/0?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8})$/.test(value);
},
message: '手机号码不正确'
},
phone: {
validator: function (value, param) {
return /^(\+\d+)?(\d{3,4}\-?)?\d{7,8}$/.test(value);
},
message: '座机号码不正确'
},
mobileorphone: {
validator: function (value, param) {
return /^((\+\d+)?(\d{3,4}\-?)?\d{7,8})|(0?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8})$/.test(value);
},
message: '电话号码不正确'
},
fax: {
validator: function (value, param) {
return /^(\d{3,4}-)?\d{7,8}$/.test(value);
},
message: '传真号码不正确'
},
loginName: {
validator: function (value, param) {
return /^[\u0391-\uFFE5\w]+$/.test(value);
},
message: '登录名称只允许汉字、英文字母、数字及下划线。'
},
safepass: {
validator: function (value, param) {
return safePassword(value);
},
message: '密码由字母和数字组成,至少6位'
},
equalTo: {
validator: function (value, param) {
alert(param);
return value == $(param[0]).val();
},
message: '两次输入的字符不一致'
},
equalOrGreatTo: {
validator: function (value, param) {
return value >= $(param[0]).val();
},
message: '本次输入的字符和上次比较应该相同或者更大'
},
number: {
validator: function (value, param) {
return isNumber(value);
},
message: '请输入正整数字'
},
isNumber: {
validator: function (value, param) {
return /^[\-]?\d+([\.]?[\d]{1,})?$/.test(value);
},
message: '请输入数字'
},
Char: {
validator: function (value, param) {
return /^[a-zA-Z]+$/.test(value);
},
message: '请输入英文字母'
},
numberAndChar: {
validator: function (value, param) {
return /^[a-zA-Z0-9]+$/.test(value);
},
message: '请输入数字或英文字母'
},
stringsDou: {
validator: function (value, param) {
return /^[a-zA-Z0-9,]+$/.test(value);
},
message: '请输入数字或英文字母'
},
numberCharAndChs: {
validator: function (value, param) {
return /^[a-zA-Z0-9\u0391-\uFFE5]+$/.test(value);
},
message: '请输入数字或英文字母或汉字'
},
stringsDouT: {//包含小数点 add by mengxin 2014-11-13
validator: function (value, param) {
return /^[a-zA-Z0-9.\u0391-\uFFE5]+$/.test(value);
},
message: '请输入数字或英文字母'
},
idcard: {
validator: function (value, param) {
return idCard(value);
},
message:'请输入正确的身份证号码'
},
numberDouble: {
validator: function (value, param) {
if (undefined==param) {
return isNumber(value);
}
var pattern = new RegExp("^[1-9]{1}\\d*([\\.]{1}[\\d]{1,"+param+"})?$|^0$|^[0]{1}[\\.]{1}[\\d]{1,"+param+"}$");
return pattern.test(value);
},
message:'请输入小数或整数,小数点后保留{0}位'
},
exRateHL: {
validator: function (value, param) {
if (undefined==param) {
return false;
}
var pattern = new RegExp("^[1-9]{0,"+param[0]+"}([\\.]{1}[\\d]{0,"+(param[1]-1)+"})?[1-9]$|^[0]{1}[\\.]{1}[\\d]{0,"+(param[1]-1)+"}[1-9]$");
return pattern.test(value);
},
message:'汇率格式为小数点前最多1-{0}位,小数点后保留{1}位,最后一位不可为0'
},
isNotChinese: {
validator: function (value, param) {
return /[^\u4e00-\u9fa5]+$/.test(value);
},
message:'请输入非中文字符'
},
isString: { //汉字、英文、数字 、下滑线、横线、点、空格、逗号
validator: function (value, param) {
return /^[\u0391-\uFFE5\w\s-\.\,]+$/.test(value);
},
message:'输入内容含有非法字符'
},
isPercent: { //验证百分数
validator: function (value, param) {
return /^[\-]?\d+([\.]?[\d]{1,})?\%$/.test(value);
},
message:'请输入百分数'
},
isNotSelect: {
validator: function (value, param) {
return isSelect(value);
},
message:'请选择下拉选项'
},
date:{
validator: function (value, param) {
return /^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$/.test(value);
},
message:'请输入正确的日期格式'
},
date_yyyy_mm:{
validator: function (value, param) {
return /^\b[1-3]\d{3}(-|\/)(0[1-9]|1[0-2])$/.test(value);
},
message:'请输入格式为yyyy-MM(2014-01)'
},
date_yyyy_mm_dd:{
validator: function (value, param) {
return /^\b[1-3]\d{3}(-|\/)(0[1-9]|1[0-2])(-|\/)(0[1-9]|1[0-9]|2[0-9]|3[0-1])$/.test(value);
},
message:'请输入格式为yyyy-MM-dd(2014-01-01)'
},
date_yyyy_mm_dd_hh_mm :{
validator: function (value, param) {
return /^\b[1-3]\d{3}(-|\/)(0[1-9]|1[0-2])(-|\/)(0[1-9]|1[0-9]|2[0-9]|3[0-1])\s((0?[0-9])|(1[0-9]|2[0-3]))\:([0-5]?[0-9])$/.test(value);
},
message:'请输入格式为yyyy-MM-dd hh:mm(2014-01-01 00:00)'
},
comparedate : {/* 比较大小 */
validator : function(value, param) {
var msg = param[1];
var startDate = new Date(Date.parse($('#'+param[0]).datebox("getValue").replace(/-/g, "/")));
var endDate = new Date(Date.parse(value.replace(/-/g, "/")));
if(startDate.getTime() < endDate.getTime()){
return true;
} else {
$.fn.validatebox.defaults.rules.comparedate.message = msg;
return false;
}
},
message : '该输入项为必输项'
},
compare : {/* 比较大小 */
validator : function(value, param) {
var msg = param[2];
var expression = param[1];
var cv = parseFloat($('#'+param[0]).val()); //比较值
value = parseFloat(value);
if(expression == "\>"){
if(value > cv){
return true;
} else {
$.fn.validatebox.defaults.rules.compare.message = msg;
return false;
}
}else if(expression == "\>="){
if(value >= cv){
return true;
} else {
$.fn.validatebox.defaults.rules.compare.message = msg;
return false;
}
} else if(expression == "\<"){
if(value < cv){
return true;
} else {
$.fn.validatebox.defaults.rules.compare.message = msg;
return false;
}
} else if(expression == "\<="){
if(value <= cv){
return true;
} else {
$.fn.validatebox.defaults.rules.compare.message = msg;
return false;
}
} else {
if(value == cv){
return true;
} else {
$.fn.validatebox.defaults.rules.compare.message = msg;
return false;
}
}
},
message : '该输入项为必输项'
},
CJH:{//车架号验证
validator: function (value, param) {
if (value.length == 0) {
return false;
}
var result = $.ajax({
type: "POST",//http请求方式
url:purl+'cjhValidator.dh?key=checkCJH', //服务器段url地址
data:{CJH:value}, //发送给服务器段的数据
dataType: "json", //告诉JQuery返回的数据格式
async: false
}).responseText;
result = JSON.parse(result);
if (result.success == false) {
$.fn.validatebox.defaults.rules.CJH.message = result.msg;
return false;
} else {
return true;
}
},
message:'该输入项为必输项'
},
checklength: {
validator: function (value, param) {
if (value.length != param[0]) {
$.fn.validatebox.defaults.rules.checklength.message = param[1]+'的长度必须为'+param[0]+'位';
return false;
}else{
return true;
}
}
}
});
var isSelect=function (value){
if(value.indexOf("请选择")>-1)
return false;
return true;
};
var isNumber = function (value) {
return /^\d+$/.test(value);
};
var safePassword = function (value) {
return !(/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/.test(value));
};
var idCard = function (value) {
if (value.length == 18 && 18 != value.length) return false;
var number = value.toLowerCase();
var d, sum = 0, v = '10x98765432', w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], a = '11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91';
var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/);
if (re == null || a.indexOf(re[1]) < 0) return false;
if (re[2].length == 9) {
number = number.substr(0, 6) + '19' + number.substr(6);
d = ['19' + re[4], re[5], re[6]].join('-');
} else d = [re[9], re[10], re[11]].join('-');
if (!isDateTime.call(d, 'yyyy-MM-dd')) return false;
for (var i = 0; i < 17; i++) sum += number.charAt(i) * w[i];
return (re[2].length == 9 || number.charAt(17) == v.charAt(sum % 11));
};
var isDateTime = function (format, reObj) {
format = format || 'yyyy-MM-dd';
var input = this, o = {}, d = new Date();
var f1 = format.split(/[^a-z]+/gi), f2 = input.split(/\D+/g), f3 = format.split(/[a-z]+/gi), f4 = input.split(/\d+/g);
var len = f1.length, len1 = f3.length;
if (len != f2.length || len1 != f4.length) return false;
for (var i = 0; i < len1; i++) if (f3[i] != f4[i]) return false;
for (var i = 0; i < len; i++) o[f1[i]] = f2[i];
o.yyyy = s(o.yyyy, o.yy, d.getFullYear(), 9999, 4);
o.MM = s(o.MM, o.M, d.getMonth() + 1, 12);
o.dd = s(o.dd, o.d, d.getDate(), 31);
o.hh = s(o.hh, o.h, d.getHours(), 24);
o.mm = s(o.mm, o.m, d.getMinutes());
o.ss = s(o.ss, o.s, d.getSeconds());
o.ms = s(o.ms, o.ms, d.getMilliseconds(), 999, 3);
if (o.yyyy + o.MM + o.dd + o.hh + o.mm + o.ss + o.ms < 0) return false;
if (o.yyyy < 100) o.yyyy += (o.yyyy > 30 ? 1900 : 2000);
d = new Date(o.yyyy, o.MM - 1, o.dd, o.hh, o.mm, o.ss, o.ms);
var reVal = d.getFullYear() == o.yyyy && d.getMonth() + 1 == o.MM && d.getDate() == o.dd && d.getHours() == o.hh && d.getMinutes() == o.mm && d.getSeconds() == o.ss && d.getMilliseconds() == o.ms;
return reVal && reObj ? d : reVal;
function s(s1, s2, s3, s4, s5) {
s4 = s4 || 60, s5 = s5 || 2;
var reVal = s3;
if (s1 != undefined && s1 != '' || !isNaN(s1)) reVal = s1 * 1;
if (s2 != undefined && s2 != '' && !isNaN(s2)) reVal = s2 * 1;
return (reVal == s1 && s1.length != s5 || reVal > s4) ? -10000 : reVal;
}
};