-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.js
More file actions
136 lines (120 loc) · 3 KB
/
Copy pathvalidate.js
File metadata and controls
136 lines (120 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class Validate {
/**
* @description 检验手机号
* @param {any} val - {
* 移动号段:
* 134 135 136 137 138 139 147 150 151 152 157 158 159 172 178 182 183 184 187 188
* 联通号段:
* 130 131 132 145 155 156 171 175 176 185 186
* 电信号段:
* 133 149 153 173 177 180 181 189
* 虚拟运营商:
* 170
* }
*/
checkPhone = (text) => {
// 如果text为空
if (text) return;
const reg = /^(13[0-9]|14[579]|15[0-35-9]|17[01235678]|18[0-9])[0-9]{8}$/;
return !reg.test(text) ? false : true;
}
// new Date转化为yyyy-MM-dd HH:mm:ss
dateToFormat = (date) => {
return date.toLocaleString('zh-CN', {
hour12: false
})
.replace(/\//g, '-').replace(/\b\d\b/g, '0$&');
}
// new Date转化为yyyy-MM-dd
dateToDate = (date) => {
return this.dateToFormat(date).split(' ')[0];
}
// new Date转化为HH:mm:ss
dateToTime = (date) => {
return this.dateToFormat(date).split(' ')[1];
}
// 日期转化为毫秒数
forMatToTime = (date) => {
return new Date(date).getTime();
}
// 数组去重
changrReArr = (arr) => {
return Array.from(new Set(arr));
// 或者 return [...new Set(arr)];
}
// 数组排序,升序
orderArr = (arr) => {
arr.sort((a, b) => {
return a - b;
});
}
// 数组中的最大值
maxArr = (arr) => {
return Math.max(...arr);
// 或者return Math.max.apply(null, arr);
}
/**
* @description 检验val的值的长度是否与len相等
* @param {any} val - 值
* @param {number} len - 长度
*/
checkLengthEqual(val, len) {
return val.length !== len ? false : true;
}
/**
* @description 检验val的长度是否在min和max之间
* @param {any} val - 值
* @param {number} min - 最小长度
* @param {number} max - 最大长度
*/
checkLength(val, min, max) {
if (max === undefined) {
return (val.length < min) ? false : true;
}
return (val.length < min || val.length > max) ? false : true;
}
/**
* @description 格式化千分位
* @param {number} val - 值
*/
formatThousandth(val) {
let reg = /(\d)(?=(\d{3})+(?:\.\d+)?$)/g;
if (!!val) {
return val.replace(reg, '$1,');
} else {
return val;
}
}
/**
* @description 将千分位的','去掉
* @param {number} val - 值
*/
parseThousandth(val) {
return val.replace(/,/g, '');
}
/**
* @description 检验身份证号
* @param {number} val - 值
*/
checkCardID(val) {
let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
return !reg.test(val) ? false : true;
}
/**
* @description 检验银行卡号
* @param {number} val - 银行卡
*/
checkBankCard(val) {
let reg = /^\d{16}|\d{19}$/;
return !reg.test(val) ? false : true;
}
/**
* @description 检验邮箱
* @param {any} val - 值
*/
checkEmail(val) {
let reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
return !reg.test(val) ? false : true;
}
}
export default new Validate;