## 一些简单规则
~~~javascript
numeric: [/^[0-9]*$/, '请填写数值'],
money: [/^(?:0|[1-9]\d*)(?:\.\d{1,2})?$/, "请填写有效的金额"],
ip: [/^((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})$/i, '请填写有效的 IP 地址'],
~~~
## 相同 name 的字段至少填写一个
~~~javascript
requiredOne: function(element) {
return !!this.$el.find('input[name="' + element.name + '"]').filter(function() {
return !this.disabled && this.value;
}).length;
}
~~~
## 身份证
~~~javascript
// 身份证
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}(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 || "请填写正确的身份证号码";
}
~~~
## 银行卡(借记卡、贷记卡)
~~~javascript
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)`
**参数**:卡类型(不传参则支持全部类型)
支持的卡类型:visa, mastercard, amex, dinersclub, enroute, discover, jcb, unknown
~~~javascript
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 "请填写有效的信用卡号";
}
~~~
## 组织机构代码证
~~~javascript
// 组织机构代码证
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 || "请填写正确的组织机构代码";
}
~~~
## 营业执照号 (工商注册号)
~~~javascript
// 营业执照号
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 || "请填写正确的营业执照号";
}
~~~
## 统一社会信用代码(三码合一)
~~~javascript
// 统一社会信用代码
unicode: function(element) {
var value = element.value.replace(/^\s*|\s*$/g, ''),
isValid = true,
rFormat = /^[1-9A-GV][1239][1-9]\d{5}[A-Z\d]{8}[X\d][Y\d]/;
if (!rFormat.test(value)) {
isValid = false;
} else {
var code, Wi, Ci, sum, C9, C18;
// 计算组织机构代码校验位:C9 = 11 - MOD ( ∑(Ci*Wi), 11 )
code = value.slice(9,17);
Wi = [3,7,9,10,5,8,4,2];
Ci = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 加权求和
sum = 0;
for(var i = 0; i < Wi.length; i++){
sum += Ci.indexOf( code.charAt(i) ) * Wi[i];
}
C9 = 11 - (sum % 11);
if (C9===10) C9 = 'X';
else if (C9===11) C9 = 0;
C9 = ''+C9;
// 与校验位比对
if ( C9 !== code.charAt(9)) {
isValid = false;
}
if (isValid) {
// 计算最后校验位:C18 = 31 - MOD ( ∑(Ci*Wi), 31 )
code = value.slice(0,17);
Wi = [1,3,9,27,19,26,16,17,20,29,25,13,8,24,10,30,28];
Ci = "0123456789ABCDEFGHJKLMNPQRTUWXY";
// 加权求和
sum = 0;
for(var i = 0; i < Wi.length; i++){
sum += Ci.indexOf( code.charAt(i) ) * Wi[i];
}
C18 = 31 - (sum % 31);
if (C18===30) C18 = 'Y';
else if (C18===31) C18 = 0;
C18 = ''+C18;
// 与校验位比对
if ( C18 !== code.charAt(18)) {
isValid = false;
}
}
}
return isValid || "请填写正确的统一社会信用代码";
}
~~~