年月日
var checkDate = function (val) {
var pattern = /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/;
if (pattern.test(val)) {
var year = val.substring(0, 4);
var month = val.substring(4, 6);
var date = val.substring(6, 8);
var date2 = new Date(year + '-' + month + '-' +date);
if(date2 && date2.getMonth() == (parseInt(month)-1) ) {
return true;
}
}
return false;
}
身份证
var checkCode = function (val) {
var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
var code = val.substring(17);
if (p.test(val)) {
var sum = 0;
for(var i=0;i<17;i++) {
sum += val[i]*factor[i];
}
if(parity[sum % 11] == code.toUpperCase()) {
return true;
}
}
return false;
}
银行卡
var verifyBankID = function (str) {
var strArr = str.split('').reverse(),
oddArr = [],
evenArr = [],
oddRes = 0,
evenRes = 0;
$.each(strArr, function(i) {
if ((i + 1) % 2 == 0) {
evenArr.push(strArr[i] * 2)
} else {
oddArr.push(strArr[i] * 1);
}
});
$.each(oddArr.join('').split(''), function() {
oddRes += this * 1;
});
$.each(evenArr.join('').split(''), function() {
evenRes += this * 1;
});
if ((oddRes + evenRes) % 10 == 0) {
return 1;
} else {
return 0;
}
}
电话正则
phone = /^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\d{8}$/;
邮箱正则
email = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[a-zA-Z]{2,3}$/;