Closed niceue closed 8 years ago
说明:相同name的多个字段必须至少填写一个
规则:requiredOne
参数:无
requiredOne: function(element) {
return !!this.$el.find('input[name="' + element.name + '"]').filter(function() {
return !this.disabled && this.value;
}).length;
}
身份证 格式校验
// 身份证
idcard: function(element) {
var value = element.value,
isValid = true;
var cityCode = {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:"国外 "};
/* 15位校验规则: (dddddd yymmdd xx g) g奇数为男,偶数为女
* 18位校验规则: (dddddd yyyymmdd xxx p) xxx奇数为男,偶数为女,p校验位
校验位公式:C17 = C[ MOD( ∑(Ci*Wi), 11) ]
i----表示号码字符从由至左包括校验码在内的位置序号
Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1
Ci 1 0 X 9 8 7 6 5 4 3 2
*/
var rFormat =/^\d{6}(18|19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$|^\d{6}\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}$/; // 格式验证
if ( !rFormat.test(value) || !cityCode[value.substr(0,2)] ) {
isValid = false;
}
// 18位身份证需要验证最后一位校验位
else if (value.length === 18) {
var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ]; // 加权因子
var Ci = "10X98765432"; // 校验字符
// 加权求和
var sum = 0;
for (var i = 0; i < 17; i++) {
sum += value.charAt(i) * Wi[i];
}
// 计算校验值
var C17 = Ci.charAt(sum % 11);
// 与校验位比对
if ( C17 !== value.charAt(17) ) {
isValid =false;
}
}
return isValid || "请输入正确的身份证号码";
}
组织机构代码证
// 组织机构代码证
orgcode: function(element) {
var value = element.value,
isValid = true,
rFormat = /^[A-Z\d]{8}-[X\d]/;
if (!rFormat.test(value)) {
isValid = false;
} else {
var Wi = [3,7,9,10,5,8,4,2];
var Ci = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 加权求和
var sum = 0;
for(var i = 0; i < 8; i++){
sum += Ci.indexOf( value.charAt(i) ) * Wi[i];
}
// 计算校验值: C9 = 11 - MOD ( ∑(Ci*Wi), 11 )
var C9 = 11 - (sum % 11);
if (C9===10) C9 = 'X';
else if (C9===11) C9 = 0;
C9 = ''+C9;
// 与校验位比对
if ( C9 !== value.charAt(9)) {
isValid = false;
}
}
return isValid || "请输入正确的组织机构代码";
}
营业执照号 (工商注册号)
// 营业执照号
bizcode: function(element) {
var value = element.value,
isValid = true,
rFormat = /^[1-6]\d{14}$/;
// 共15位:6位首次登记机关代码 + 8位顺序码 + 校验位
if (!rFormat.test(value)) {
isValid = false;
} else {
var s = [],
p = [10];
for (var i=0; i<15; i++) {
s[i] = ( p[i] % 11 ) + (+value.charAt(i));
p[i+1] = (s[i] % 10 || 10) * 2;
}
if (1 !== s[14] % 10) {
isValid = false;
}
}
return isValid || "请输入正确的营业执照号";
}
银行卡(借记卡、贷记卡)
bankcard: function(element) {
var value = element.value.replace(/\s/g, ''),
isValid = true,
rFormat = /^[\d]{12,19}$/;
if ( !rFormat.test(value) ) {
isValid = false;
} else {
var arr = value.split('').reverse(),
i = arr.length,
temp,
sum = 0;
while ( i-- ) {
if ( i%2 === 0 ) {
sum += +arr[i];
} else {
temp = +arr[i] * 2;
sum += temp % 10;
if ( temp > 9 ) sum += 1;
}
}
if ( sum % 10 !== 0 ) {
isValid = false;
}
}
return isValid || "请输入有效的银行卡号";
}
说明:信用卡
规则:creditcard(type1, type2, ... typen)
参数:卡类型(不传参则支持全部类型)
支持的卡类型:mastercard, visa, amex, dinersclub, enroute, discover, jcb, unknown
creditcard: function(element, params) {
var value = element.value,
validTypes = 0x0000,
types = {
mastercard: 0x0001,
visa: 0x0002,
amex: 0x0004,
dinersclub: 0x0008,
enroute: 0x0010,
discover: 0x0020,
jcb: 0x0040,
unknown: 0x0080
};
if (/[^0-9\-]+/.test(value)) {
return false;
}
value = value.replace(/\D/g, "");
if ( !params ) {
validTypes = 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080;
} else {
for (var i=0; i<parmas.length; i++) {
validTypes |= types[params[i]];
}
}
if (validTypes & 0x0001 && /^(5[12345])/.test(value)) { //mastercard
return value.length === 16;
}
if (validTypes & 0x0002 && /^(4)/.test(value)) { //visa
return value.length === 16;
}
if (validTypes & 0x0004 && /^(3[47])/.test(value)) { //amex
return value.length === 15;
}
if (validTypes & 0x0008 && /^(3(0[012345]|[68]))/.test(value)) { //dinersclub
return value.length === 14;
}
if (validTypes & 0x0010 && /^(2(014|149))/.test(value)) { //enroute
return value.length === 15;
}
if (validTypes & 0x0020 && /^(6011)/.test(value)) { //discover
return value.length === 16;
}
if (validTypes & 0x0040 && /^(3)/.test(value)) { //jcb
return value.length === 16;
}
if (validTypes & 0x0040 && /^(2131|1800)/.test(value)) { //jcb
return value.length === 15;
}
if (validTypes & 0x0080) { //unknown
return true;
}
return false;
}
注:从jQuery Validation插件中的规则转换而来
欢迎贡献出有用的规则。三木成森,群众的力量是无穷的! :+1: