/*
validform datatype extension
by sean during december 8, 2012 - february 20, 2013
for more information, please visit http://validform.rjboy.cn
扩展以下类型:
date:匹配日期
zh:匹配中文字符
dword:匹配双字节字符
money:匹配货币类型
ipv4:匹配ipv4地址
ipv6:匹配ipv6地址
num:匹配数值型
qq:匹配qq号码
unequal:当前值不能等于被检测的值,如可以用来检测新密码不能与旧密码一样
notvalued:当前值不能包含指定值,如密码不能包含用户名等的检测
min:多选框最少选择多少项
max:多选框最多不能超过多少项
byterange:判断字符长度,中文算两个字符
numrange:判断数值范围,如小于100大于10之间的数
daterange:判断日期范围
idcard:对身份证号码进行严格验证
*/
(function () {
if ($.datatype) {
$.extend($.tipmsg.w, {
"date": "请填写日期!",
"datetime": "请选择时间!",
"zh": "请填写中文!",
"dword": "请填写双字节字符!",
"money": "请填写货币值!",
"ipv4": "请填写ip地址!",
"ipv6": "请填写ipv6地址!",
"num": "请填写数值!",
"qq": "请填写qq号码!",
"unequal": "值不能相等!",
"notvalued": "不能含有特定值!",
"idcard": "身份证号码不对!",
"url": "url地址不正确!",
"bankcard": "请填写银行卡号!"
});
$.extend($.datatype, {
/*
reference http://blog.csdn.net/lxcnn/article/details/4362500;
日期格式可以是:20120102 / 2012.01.02 / 2012/01/02 / 2012-01-02
时间格式可以是:10:01:10 / 02:10
如 2012-01-02 02:10
2012-01-02
*/
"date": /^(?:(?:1[6-9]|[2-9][0-9])[0-9]{2}([-/.]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:(?:1[6-9]|[2-9][0-9])(?:0[48]|[2468][048]|[13579][26])|(?:16|[2468][048]|[3579][26])00)([-/.]?)0?2\2(?:29))(\s+([01][0-9]:|2[0-3]:)?[0-5][0-9]:[0-5][0-9])?$/,
"datetime": /^\d{4}\-\d{1,2}\-\d{1,2}\s{1}(\d{1,2}:){2}\d{1,2}$/,
//匹配中文字符;
"zh": /^[\u4e00-\u9fa5]+$/,
//匹配双字节字符;
"dword": /^[^\x00-\xff]+$/,
//货币类型;
"money": /^([\u0024\u00a2\u00a3\u00a4\u20ac\u00a5\u20b1\20b9\uffe5]\s*)(\d+,?)+\.?\d*\s*$/,
//匹配ipv4地址;
"ipv4": /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/,
/*
匹配ipv6地址;
reference http://forums.intermapper.com/viewtopic.php?t=452;
*/
"ipv6": /^\s*((([0-9a-fa-f]{1,4}:){7}([0-9a-fa-f]{1,4}|:))|(([0-9a-fa-f]{1,4}:){6}(:[0-9a-fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-fa-f]{1,4}:){5}(((:[0-9a-fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-fa-f]{1,4}:){4}(((:[0-9a-fa-f]{1,4}){1,3})|((:[0-9a-fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-fa-f]{1,4}:){3}(((:[0-9a-fa-f]{1,4}){1,4})|((:[0-9a-fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-fa-f]{1,4}:){2}(((:[0-9a-fa-f]{1,4}){1,5})|((:[0-9a-fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-fa-f]{1,4}:){1}(((:[0-9a-fa-f]{1,4}){1,6})|((:[0-9a-fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-fa-f]{1,4}){1,7})|((:[0-9a-fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/,
"url": /^((https|http):\/\/)?[a-z0-9a-z]{3}\.[a-z0-9a-z][a-z0-9a-z]{0,61}?[a-z0-9a-z]\.com|net|cn|cc (:s[0-9]{1-4})?$/,
//数值型;
"num": /^(\d+[\s,]*)+\.?\d*$/,
//银行卡
"bankcard": /^(\d+){16,19}$/,
//qq号码;
"qq": /^[1-9][0-9]{4,}$/,
/*
参数gets是获取到的表单元素值,
obj为当前表单元素,
curform为当前验证的表单,
datatype为内置的一些正则表达式的引用。
*/
"unequal": function (gets, obj, curform, datatype) {
/*
当前值不能与指定表单元素的值一样,如新密码不能与旧密码一样,密码不能设置为用户名等
注意需要通过绑定with属性来指定要比较的表单元素,可以是clas,id或者是name属性值
eg.
eg1.
eg2.
eg3.
也可以用来验证不能与with指定的值相等
当上面根据class,id和name都查找不到对象时,会直接跟with的值比较
eg4.
该文本框的值不能等于100
*/
var withele = $.trim(obj.attr("with"));
var val = curform.find(withele + ",[name='" + withele + "']").val() || withele;
if (gets == $.trim(val)) {
return false;
}
},
"notvalued": function (gets, obj, curform, datatype) {
/*
当前文本框的值不能含有指定文本框的值,如注册时设置的密码里不能包含用户名
注意需要给表单元素绑定with属性来指定要比较的表单元素,可以是clas,id或者是name属性值
eg.
也可以用来验证不能包含with指定的值
当上面根据class,id和name都查找不到对象时,会直接跟with的值比较
eg2.
要求不能含有"validform"字符
*/
var withele = $.trim(obj.attr("with"));
var val = curform.find(withele + ",[name='" + withele + "']").val() || withele;
if (gets.indexof($.trim(val)) != -1) {
return false;
}
},
"min": function (gets, obj, curform, datatype) {
/*
checkbox最少选择n项
注意需要给表单元素绑定min属性来指定是至少需要选择几项,没有绑定的话使用默认值
eg.
*/
var minim = ~~obj.attr("min") || 2,
numselected = curform.find("input[name='" + obj.attr("name") + "']:checked").length;
return numselected >= minim ? true : "请至少选择" + minim + "项!";
},
"max": function (gets, obj, curform, datatype) {
/*
checkbox最多选择n项
注意需要给表单元素绑定max属性来指定是最多需要选择几项,没有绑定的话使用默认值
eg.
*/
var atmax = ~~obj.attr("max") || 2,
numselected = curform.find("input[name='" + obj.attr("name") + "']:checked").length;
if (numselected == 0) {
return false;
} else if (numselected > atmax) {
return "最多只能选择" + atmax + "项!";
}
return true;
},
"byterange": function (gets, obj, curform, datatype) {
/*
判断字符长度,中文算两个字符
注意需要给表单元素绑定max,min属性来指定最大或最小允许的字符长度,没有绑定的话使用默认值
*/
var dregx = /[^\x00-\xff]/g;
var maxim = ~~obj.attr("max") || 100000000,
minim = ~~obj.attr("min") || 0;
getslen = gets.replace(dregx, "00").length;
if (getslen > maxim) {
return "输入字符不能多于" + maxim + "个,中文算两个字符!";
}
if (getslen < minim) {
return "输入字符不能少于" + minim + "个,中文算两个字符!";
}
return true;
},
"numrangecontrastr": function (gets, obj, curform, datatype) {
gets = gets.replace(/\s*/g, "").replace(/,/g, "");
if (!/^\d+\.?\d*$/.test(gets)) {
return "只能输入数字!";
}
var _maxid = obj.attr("maxid");
var _minid = obj.attr("minid");
if (_maxid != undefined) {
//当前为最小值
var maxvalue = $("#" + _maxid).val();
var max = parseint(maxvalue);
if (max == 0 || isnan(max)) {
$("#" + _maxid).focus();
return "请输入最大值!";
}
if (gets > max) {
return "最小值大于最大值!";
}
//$("#"+_maxid).focus();
} else if (_minid != undefined) {
//当前为最大值
var minvalue = $("#" + _minid).val();
var min = parseint(minvalue);
if (min == nan) {
$("#" + _minid).focus();
return "请输入最小值!";
}
if (gets < min) {
return "最大值小于最小值!";
}
//$("#" + _minid).focus();
} else {
return "参数错误";
}
return true;
},
"numrange": function (gets, obj, curform, datatype) {
/*
判断数值范围
注意需要给表单元素绑定max,min属性来指定最大或最小可输入的值,没有绑定的话使用默认值
*/
var maxim = ~~obj.attr("max") || 100000000,
minim = ~~obj.attr("min") || 0;
if (obj.attr("min") != undefined && obj.attr("min").length > 0) {
minim = parsefloat(obj.attr("min"));
if (isnan(minim)) {
minim = 0;
}
}
if (obj.attr("max") != undefined && obj.attr("max").length > 0) {
maxim = parsefloat(obj.attr("max"));
if (isnan(maxim)) {
maxim = 100000000;
}
}
gets = gets.replace(/\s*/g, "").replace(/,/g, "");
if (!/^\d+\.?\d*$/.test(gets)) {
return "只能输入数字!";
}
if (gets < minim) {
return "值不能小于" + minim + "!";
} else if (gets > maxim) {
return "值不能大于" + maxim + "!";
}
return true;
},
"daterange": function (gets, obj, curform, datatype) {
/*
判断日期范围
注意需要给表单元素绑定max或min属性,或两个同时绑定来指定最大或最小可输入的日期
日期格式:2012/12/29 或 2012-12-29 或 2012.12.29 或 2012,12,29
*/
var maxim = new date(obj.attr("max").replace(/[-\.,]/g, "/")),
minim = new date(obj.attr("min").replace(/[-\.,]/g, "/")),
gets = new date(gets.replace(/[-\.,]/g, "/"));
if (!gets.getdate()) {
return "日期格式不对!";
}
if (gets > maxim) {
return "日期不能大于" + obj.attr("max");
}
if (gets < minim) {
return "日期不能小于" + obj.attr("min");
}
return true;
},
"idcard": function (gets, obj, curform, datatype) {
/*
该方法由网友提供;
对身份证进行严格验证;
*/
var wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];// 加权因子;
var validecode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];// 身份证验证位值,10代表x;
if (gets.length == 15) {
return isvaliditybrithby15idcard(gets);
} else if (gets.length == 18) {
var a_idcard = gets.split("");// 得到身份证数组
if (isvaliditybrithby18idcard(gets) && istruevalidatecodeby18idcard(a_idcard)) {
return true;
}
return false;
}
return false;
function istruevalidatecodeby18idcard(a_idcard) {
var sum = 0; // 声明加权求和变量
if (a_idcard[17].tolowercase() == 'x') {
a_idcard[17] = 10;// 将最后位为x的验证码替换为10方便后续操作
}
for (var i = 0; i < 17; i++) {
sum += wi[i] * a_idcard[i];// 加权求和
}
valcodeposition = sum % 11;// 得到验证码所位置
if (a_idcard[17] == validecode[valcodeposition]) {
return true;
}
return false;
}
function isvaliditybrithby18idcard(idcard18) {
var year = idcard18.substring(6, 10);
var month = idcard18.substring(10, 12);
var day = idcard18.substring(12, 14);
var temp_date = new date(year, parsefloat(month) - 1, parsefloat(day));
// 这里用getfullyear()获取年份,避免千年虫问题
if (temp_date.getfullyear() != parsefloat(year) || temp_date.getmonth() != parsefloat(month) - 1 || temp_date.getdate() != parsefloat(day)) {
return false;
}
return true;
}
function isvaliditybrithby15idcard(idcard15) {
var year = idcard15.substring(6, 8);
var month = idcard15.substring(8, 10);
var day = idcard15.substring(10, 12);
var temp_date = new date(year, parsefloat(month) - 1, parsefloat(day));
// 对于老身份证中的你年龄则不需考虑千年虫问题而使用getyear()方法
if (temp_date.getyear() != parsefloat(year) || temp_date.getmonth() != parsefloat(month) - 1 || temp_date.getdate() != parsefloat(day)) {
return false;
}
return true;
}
}
});
} else {
settimeout(arguments.callee, 10);
}
})();