151 lines
4.3 KiB
JavaScript
151 lines
4.3 KiB
JavaScript
|
/**
|
|||
|
* 正则匹配工具类
|
|||
|
*/
|
|||
|
export default class Regular {
|
|||
|
|
|||
|
/**
|
|||
|
* 图片链接验证
|
|||
|
* @param {String} url 图片链接
|
|||
|
*/
|
|||
|
static isPicture(url) {
|
|||
|
var strRegex = /.*(\.png|\.jpg|\.jpeg|\.gif)$/
|
|||
|
var re = new RegExp(strRegex);
|
|||
|
if (re.test(url)) {
|
|||
|
return true;
|
|||
|
} else {
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* url验证
|
|||
|
* @param {Object} value
|
|||
|
*/
|
|||
|
static isURL(value) {
|
|||
|
var strRegex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/
|
|||
|
var re = new RegExp(strRegex);
|
|||
|
if (re.test(value)) {
|
|||
|
return true;
|
|||
|
} else {
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
/**
|
|||
|
* 银行卡验证-简单版
|
|||
|
* @param {Object} bankNo 银行卡号
|
|||
|
*
|
|||
|
*/
|
|||
|
/*
|
|||
|
let reg = /^(.{4})(?:\d+)(.{4})$/
|
|||
|
let bankNos = '6222388202014638633'
|
|||
|
let str = bankNos.replace(reg, "$1 **** **** $2")
|
|||
|
console.log('输出结果:', str) //输出结果: 6222 **** **** 8633
|
|||
|
*/
|
|||
|
static luhnCheck(bankno) {
|
|||
|
var lastNum = bankno.substr(bankno.length - 1, 1); //取出最后一位(与luhn进行比较)
|
|||
|
var first15Num = bankno.substr(0, bankno.length - 1); //前15或18位
|
|||
|
var newArr = new Array();
|
|||
|
for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组
|
|||
|
newArr.push(first15Num.substr(i, 1));
|
|||
|
}
|
|||
|
var arrJiShu = new Array(); //奇数位*2的积 <9
|
|||
|
var arrJiShu2 = new Array(); //奇数位*2的积 >9
|
|||
|
var arrOuShu = new Array(); //偶数位数组
|
|||
|
for (var j = 0; j < newArr.length; j++) {
|
|||
|
if ((j + 1) % 2 == 1) { //奇数位
|
|||
|
if (parseInt(newArr[j]) * 2 < 9) arrJiShu.push(parseInt(newArr[j]) * 2);
|
|||
|
else arrJiShu2.push(parseInt(newArr[j]) * 2);
|
|||
|
} else //偶数位
|
|||
|
arrOuShu.push(newArr[j]);
|
|||
|
}
|
|||
|
|
|||
|
var jishu_child1 = new Array(); //奇数位*2 >9 的分割之后的数组个位数
|
|||
|
var jishu_child2 = new Array(); //奇数位*2 >9 的分割之后的数组十位数
|
|||
|
for (var h = 0; h < arrJiShu2.length; h++) {
|
|||
|
jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
|
|||
|
jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
|
|||
|
}
|
|||
|
|
|||
|
var sumJiShu = 0; //奇数位*2 < 9 的数组之和
|
|||
|
var sumOuShu = 0; //偶数位数组之和
|
|||
|
var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
|
|||
|
var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
|
|||
|
var sumTotal = 0;
|
|||
|
for (var m = 0; m < arrJiShu.length; m++) {
|
|||
|
sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
|
|||
|
}
|
|||
|
|
|||
|
for (var n = 0; n < arrOuShu.length; n++) {
|
|||
|
sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
|
|||
|
}
|
|||
|
|
|||
|
for (var p = 0; p < jishu_child1.length; p++) {
|
|||
|
sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
|
|||
|
sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
|
|||
|
}
|
|||
|
//计算总和
|
|||
|
sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
|
|||
|
|
|||
|
//计算luhn值
|
|||
|
var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
|
|||
|
var luhn = 10 - k;
|
|||
|
|
|||
|
if (lastNum == luhn) {
|
|||
|
return true;
|
|||
|
} else {
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 邮箱验证-简单版
|
|||
|
* @param {Object} email 邮箱
|
|||
|
*/
|
|||
|
static isEmail(emailVal) {
|
|||
|
const reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
|
|||
|
if (!reg.test(emailVal)) {
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* 手机号码验证-简单版
|
|||
|
* @param {Object} phoneNum 手机号码
|
|||
|
*/
|
|||
|
static isPhoneNumberSimple(phoneNum) {
|
|||
|
const reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
|
|||
|
return reg.test(phoneNum);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 手机号码验证-严谨版
|
|||
|
* @param {Object} phoneNum 手机号码
|
|||
|
*/
|
|||
|
static isPhoneNumber(phoneNum) {
|
|||
|
/*
|
|||
|
* 移动号码包括的号段:134/135/136/137,138,139;
|
|||
|
* 147/148(物联卡号);
|
|||
|
* 150/151/152/157/158/159;
|
|||
|
* 165(虚拟运营商);
|
|||
|
* 1703/1705/1706(虚拟运营商)、178;
|
|||
|
* 182/183/184/187/188
|
|||
|
* 198
|
|||
|
* 联通号段包括:130/131
|
|||
|
* 145
|
|||
|
* 155/156
|
|||
|
* 166/167(虚拟运营商)
|
|||
|
* 1704/1707/1708/1709、171
|
|||
|
* 186/186
|
|||
|
* 电信号段包括:133
|
|||
|
* 153
|
|||
|
* 162(虚拟运营商)
|
|||
|
* 1700/1701/1702(虚拟运营商)
|
|||
|
* 180/181/189
|
|||
|
* 191/199
|
|||
|
*/
|
|||
|
const reg = /^1(3[0-9]|4[5,7]|5[0,1,2,3,5,6,7,8,9]|6[2,5,6,7]|7[0,1,7,8]|8[0-9]|9[1,8,9])\d{8}$/;
|
|||
|
return reg.test(phoneNum);
|
|||
|
}
|
|||
|
}
|