lyon-fn
Version:
常用方法的封装
245 lines (238 loc) • 8.16 kB
JavaScript
// 封装各类常用方法大全
const validatedPhoneNum = value => {
let passengerPhoneTest = (/^1[1-9][0-9]\d{8}$/).test(value);
if (value.length === 0) {
return 0;
} else if (passengerPhoneTest === false) {
return 1;
} else if (passengerPhoneTest === true) {
return 2;
}
}
const validatedCode = value => {
const emailTest = ((/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/).test(value));
if (value.length === 0) {
return 0;
} else if (value.length !== 6) {
return 1;
} else if (value.length === 6) {
return 2;
}
}
const validatedUsername = value => {
if (value.length === 0) {
return 0;
} else {
return 1;
}
}
const validatedCardId = value => {
let idCardTest = ((/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/).test(value));
if (value.length === 0) {
return 0;
} else if (idCardTest === false) {
return 1;
} else if (idCardTest === true) {
return 2;
}
}
const validatedEmail = value => {
const emailTest = (/^[\w\+\-]+(\.[\w\+\-]+)*@[a-z\d\-]+(\.[a-z\d\-]+)*\.([a-z]{2,4})$/i).test(value);
if (value.length === 0) {
return 0;
} else if (emailTest === false) {
return 1;
} else if (emailTest === true) {
return 2;
}
}
// 身份证
const validatedCardIds = idcard => {
idcard = idcard.toUpperCase();
var Errors = [true, false, false, false, false];
// var Errors = [1, "身份证号码位数不对", "身份证号码出生日期超出范围或含有非法字符",
// "身份证号码校验错误", "身份证地区非法"
// ];
var area = {
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 Y, JYM;
var S, M;
var idcard_array = [];
idcard_array = idcard.split("");
var ereg, eregNow;
if (area[parseInt(idcard.substr(0, 2))] == null)
return Errors[4];
switch (idcard.length) {
case 15:
if ((parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 || ((parseInt(idcard.substr(6, 2)) + 1900) % 100 == 0 && (parseInt(idcard
.substr(6, 2)) + 1900) % 4 == 0)) {
ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/; // 测试出生日期的合法性
} else {
ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/; // 测试出生日期的合法性
}
if (ereg.test(idcard))
return Errors[0];
else
return Errors[2];
break;
case 18:
if (parseInt(idcard.substr(6, 4)) % 4 == 0 || (parseInt(idcard.substr(6, 4)) % 100 == 0 && parseInt(idcard
.substr(6, 4)) % 4 == 0)) {
ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/; // 闰年出生日期的合法性正则表达式
eregNow = /^[1-9][0-9]{5}20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/; // 闰年出生日期的合法性正则表达式
} else {
ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/; // 平年出生日期的合法性正则表达式
eregNow = /^[1-9][0-9]{5}20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/; // 平年出生日期的合法性正则表达式
}
if (ereg.test(idcard) || eregNow.test(idcard)) {
S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9 + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10 + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5 + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8 + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4 + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2 + parseInt(idcard_array[7]) * 1 + parseInt(idcard_array[8]) * 6 + parseInt(idcard_array[9]) * 3;
Y = S % 11;
M = "F";
JYM = "10X98765432";
M = JYM.substr(Y, 1);
if (M == idcard_array[17])
return Errors[0];
else
return Errors[3];
} else
return Errors[2];
break;
default:
return Errors[1];
break;
}
}
//香港身份证
const isHKID = (str) => {
var strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if (str.length < 8)
return false;
if (str.charAt(str.length - 3) == '(' && str.charAt(str.length - 1) == ')')
str = str.substring(0, str.length - 3) + str.charAt(str.length - 2);
str = str.toUpperCase();
var hkidPat = /^([A-Z]{1,2})([0-9]{6})([A0-9])$/;
var matchArray = str.match(hkidPat);
if (matchArray == null)
return false;
var charPart = matchArray[1];
var numPart = matchArray[2];
var checkDigit = matchArray[3];
var checkSum = 0;
if (charPart.length == 2) {
checkSum += 9 * (10 + strValidChars.indexOf(charPart.charAt(0)));
checkSum += 8 * (10 + strValidChars.indexOf(charPart.charAt(1)));
} else {
checkSum += 9 * 36;
checkSum += 8 * (10 + strValidChars.indexOf(charPart));
}
for (var i = 0, j = 7; i < numPart.length; i++, j--)
checkSum += j * numPart.charAt(i);
var remaining = checkSum % 11;
var verify = remaining == 0 ? 0 : 11 - remaining;
return verify == checkDigit || (verify == 10 && checkDigit == 'A');
}
const isMacauID = (idNum)=>{
const reg = /^[1|5|7][0-9]{6}\([0-9Aa]\)/;
return reg.test(idNum);
}
//户口簿
const registeredPermanentResidence = (idNum)=>{
return /^[a-zA-Z0-9]{3,21}$/.test(idNum);
}
//外国人永久居住证
const isResidence = (idNum) => {
var pattern = /[a-zA-Z0-9]{15}$/g;
if (!pattern.test(idNum)) return false;
return true;
}
// 护照验证
const isPassport = (idNum) => {
return /(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{2,18}$/.test(idNum);
}
//军官证或士兵证
const isOfficer = (idNum) => {
return /^[a-zA-Z0-9]{7,21}$/.test(idNum);
}
//军人证验证
const isSoldiers = (val) => {
var flag;
if (/^[A-Za-z]{10,18}$/g.test(val) || /^[0-9]{10,18}$/g.test(val)) {
flag = false;
} else {
flag = /[a-zA-Z0-9]{10,18}/g.test(val);
}
return flag;
}
//台胞证验证
const isTaiwan = (val) => {
return (/[0-9]{7}/g.test(val));
}
//驾照
const isDriverLicense = (idNum) => {
const reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/
return reg.test(idNum);
}
//包含数字、字母、汉字
const isNumLetterChina = function(idNum){
const reg = /^[A-Za-z0-9\u4e00-\u9fa5]+$/;
return reg.test(idNum);
}
//只能输入两位小数点的数字
const checkNumberDot = function(num){
const reg = /^\d*(?:\.\d{0,2})?$/;
return reg.test(num);
}
module.exports = {
validatedPhoneNum,
validatedCode,
validatedUsername,
validatedCardId,
validatedEmail,
validatedCardIds,
isHKID,
isMacauID,
registeredPermanentResidence,
isResidence,
isPassport,
isOfficer,
isSoldiers,
isTaiwan,
isDriverLicense,
isNumLetterChina,
checkNumberDot
}