|
@@ -0,0 +1,304 @@
|
|
1
|
+/**
|
|
2
|
+ * Created by jiachenpan on 16/11/18.
|
|
3
|
+ */
|
|
4
|
+
|
|
5
|
+export function isvalidUsername(str) {
|
|
6
|
+ const validMap = ["admin", "editor"];
|
|
7
|
+ return validMap.indexOf(str.trim()) >= 0;
|
|
8
|
+}
|
|
9
|
+
|
|
10
|
+/* 合法uri */
|
|
11
|
+export function validateURL(textval) {
|
|
12
|
+ const urlregex =
|
|
13
|
+ /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
|
|
14
|
+ return urlregex.test(textval);
|
|
15
|
+}
|
|
16
|
+/**
|
|
17
|
+ * 邮箱
|
|
18
|
+ * @param {*} s
|
|
19
|
+ */
|
|
20
|
+export function isEmail(s) {
|
|
21
|
+ return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(
|
|
22
|
+ s
|
|
23
|
+ );
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+/**
|
|
27
|
+ * 手机号码
|
|
28
|
+ * @param {*} s
|
|
29
|
+ */
|
|
30
|
+export function isMobile(s) {
|
|
31
|
+ return /^1[0-9]{10}$/.test(s);
|
|
32
|
+}
|
|
33
|
+
|
|
34
|
+/**
|
|
35
|
+ * 电话号码
|
|
36
|
+ * @param {*} s
|
|
37
|
+ */
|
|
38
|
+export function isPhone(s) {
|
|
39
|
+ return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s);
|
|
40
|
+}
|
|
41
|
+
|
|
42
|
+/**
|
|
43
|
+ * URL地址
|
|
44
|
+ * @param {*} s
|
|
45
|
+ */
|
|
46
|
+export function isURL(s) {
|
|
47
|
+ return /^http[s]?:\/\/.*/.test(s);
|
|
48
|
+}
|
|
49
|
+
|
|
50
|
+/* 小写字母 */
|
|
51
|
+export function validateLowerCase(str) {
|
|
52
|
+ const reg = /^[a-z]+$/;
|
|
53
|
+ return reg.test(str);
|
|
54
|
+}
|
|
55
|
+
|
|
56
|
+/* 大写字母 */
|
|
57
|
+export function validateUpperCase(str) {
|
|
58
|
+ const reg = /^[A-Z]+$/;
|
|
59
|
+ return reg.test(str);
|
|
60
|
+}
|
|
61
|
+
|
|
62
|
+/* 大小写字母 */
|
|
63
|
+export function validatAlphabets(str) {
|
|
64
|
+ const reg = /^[A-Za-z]+$/;
|
|
65
|
+ return reg.test(str);
|
|
66
|
+}
|
|
67
|
+/* 验证pad还是pc */
|
|
68
|
+export const vaildatePc = function () {
|
|
69
|
+ const userAgentInfo = navigator.userAgent;
|
|
70
|
+ const Agents = [
|
|
71
|
+ "Android",
|
|
72
|
+ "iPhone",
|
|
73
|
+ "SymbianOS",
|
|
74
|
+ "Windows Phone",
|
|
75
|
+ "iPad",
|
|
76
|
+ "iPod"
|
|
77
|
+ ];
|
|
78
|
+ let flag = true;
|
|
79
|
+ for (var v = 0; v < Agents.length; v++) {
|
|
80
|
+ if (userAgentInfo.indexOf(Agents[v]) > 0) {
|
|
81
|
+ flag = false;
|
|
82
|
+ break;
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+ return flag;
|
|
86
|
+};
|
|
87
|
+/**
|
|
88
|
+ * validate email
|
|
89
|
+ * @param email
|
|
90
|
+ * @returns {boolean}
|
|
91
|
+ */
|
|
92
|
+export function validateEmail(email) {
|
|
93
|
+ const re =
|
|
94
|
+ /^(([^<>()\\[\]\\.,;:\s@"]+(\.[^<>()\\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
95
|
+ return re.test(email);
|
|
96
|
+}
|
|
97
|
+
|
|
98
|
+/**
|
|
99
|
+ * 判断身份证号码
|
|
100
|
+ */
|
|
101
|
+export function cardid(code) {
|
|
102
|
+ let list = [];
|
|
103
|
+ let result = true;
|
|
104
|
+ let msg = "";
|
|
105
|
+ var city = {
|
|
106
|
+ 11: "北京",
|
|
107
|
+ 12: "天津",
|
|
108
|
+ 13: "河北",
|
|
109
|
+ 14: "山西",
|
|
110
|
+ 15: "内蒙古",
|
|
111
|
+ 21: "辽宁",
|
|
112
|
+ 22: "吉林",
|
|
113
|
+ 23: "黑龙江 ",
|
|
114
|
+ 31: "上海",
|
|
115
|
+ 32: "江苏",
|
|
116
|
+ 33: "浙江",
|
|
117
|
+ 34: "安徽",
|
|
118
|
+ 35: "福建",
|
|
119
|
+ 36: "江西",
|
|
120
|
+ 37: "山东",
|
|
121
|
+ 41: "河南",
|
|
122
|
+ 42: "湖北 ",
|
|
123
|
+ 43: "湖南",
|
|
124
|
+ 44: "广东",
|
|
125
|
+ 45: "广西",
|
|
126
|
+ 46: "海南",
|
|
127
|
+ 50: "重庆",
|
|
128
|
+ 51: "四川",
|
|
129
|
+ 52: "贵州",
|
|
130
|
+ 53: "云南",
|
|
131
|
+ 54: "西藏 ",
|
|
132
|
+ 61: "陕西",
|
|
133
|
+ 62: "甘肃",
|
|
134
|
+ 63: "青海",
|
|
135
|
+ 64: "宁夏",
|
|
136
|
+ 65: "新疆",
|
|
137
|
+ 71: "台湾",
|
|
138
|
+ 81: "香港",
|
|
139
|
+ 82: "澳门",
|
|
140
|
+ 91: "国外 "
|
|
141
|
+ };
|
|
142
|
+ if (!validatenull(code)) {
|
|
143
|
+ if (code.length === 18) {
|
|
144
|
+ if (!code || !/(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(code)) {
|
|
145
|
+ msg = "证件号码格式错误";
|
|
146
|
+ } else if (!city[code.substr(0, 2)]) {
|
|
147
|
+ msg = "地址编码错误";
|
|
148
|
+ } else {
|
|
149
|
+ // 18位身份证需要验证最后一位校验位
|
|
150
|
+ code = code.split("");
|
|
151
|
+ // ∑(ai×Wi)(mod 11)
|
|
152
|
+ // 加权因子
|
|
153
|
+ var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
|
|
154
|
+ // 校验位
|
|
155
|
+ var parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2, "x"];
|
|
156
|
+ var sum = 0;
|
|
157
|
+ var ai = 0;
|
|
158
|
+ var wi = 0;
|
|
159
|
+ for (var i = 0; i < 17; i++) {
|
|
160
|
+ ai = code[i];
|
|
161
|
+ wi = factor[i];
|
|
162
|
+ sum += ai * wi;
|
|
163
|
+ }
|
|
164
|
+ if (parity[sum % 11] !== code[17]) {
|
|
165
|
+ msg = "证件号码校验位错误";
|
|
166
|
+ } else {
|
|
167
|
+ result = false;
|
|
168
|
+ }
|
|
169
|
+ }
|
|
170
|
+ } else {
|
|
171
|
+ msg = "证件号码长度不为18位";
|
|
172
|
+ }
|
|
173
|
+ } else {
|
|
174
|
+ msg = "证件号码不能为空";
|
|
175
|
+ }
|
|
176
|
+ list.push(result);
|
|
177
|
+ list.push(msg);
|
|
178
|
+ return list;
|
|
179
|
+}
|
|
180
|
+/**
|
|
181
|
+ * 判断手机号码是否正确
|
|
182
|
+ */
|
|
183
|
+export function isvalidatemobile(phone) {
|
|
184
|
+ let list = [];
|
|
185
|
+ let result = true;
|
|
186
|
+ let msg = "";
|
|
187
|
+ var isPhone = /^0\d{2,3}-?\d{7,8}$/;
|
|
188
|
+ // 增加134 减少|1349[0-9]{7},增加181,增加145,增加17[678]
|
|
189
|
+ if (!validatenull(phone)) {
|
|
190
|
+ if (phone.length === 11) {
|
|
191
|
+ if (isPhone.test(phone)) {
|
|
192
|
+ msg = "手机号码格式不正确";
|
|
193
|
+ } else {
|
|
194
|
+ result = false;
|
|
195
|
+ }
|
|
196
|
+ } else {
|
|
197
|
+ msg = "手机号码格式不正确";
|
|
198
|
+ }
|
|
199
|
+ } else {
|
|
200
|
+ msg = "手机号码不能为空";
|
|
201
|
+ }
|
|
202
|
+ list.push(result);
|
|
203
|
+ list.push(msg);
|
|
204
|
+ return list;
|
|
205
|
+}
|
|
206
|
+/**
|
|
207
|
+ * 判断手机号码是否正确 不是必填
|
|
208
|
+ */
|
|
209
|
+export function check_phone(phone) {
|
|
210
|
+ let list = [];
|
|
211
|
+ let result = true;
|
|
212
|
+ let msg = "";
|
|
213
|
+ var isPhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
|
|
214
|
+ if (!validatenull(phone)) {
|
|
215
|
+ if (phone.length === 11) {
|
|
216
|
+ if (!isPhone.test(phone)) {
|
|
217
|
+ msg = "手机号码格式不正确";
|
|
218
|
+ } else {
|
|
219
|
+ result = false;
|
|
220
|
+ }
|
|
221
|
+ } else {
|
|
222
|
+ msg = "手机号码格式不正确";
|
|
223
|
+ }
|
|
224
|
+ } else {
|
|
225
|
+ result = false;
|
|
226
|
+ }
|
|
227
|
+ list.push(result);
|
|
228
|
+ list.push(msg);
|
|
229
|
+ return list;
|
|
230
|
+}
|
|
231
|
+/**
|
|
232
|
+ * 判断姓名是否正确
|
|
233
|
+ */
|
|
234
|
+export function validatename(name) {
|
|
235
|
+ var regName = /^[\u4e00-\u9fa5]{2,4}$/;
|
|
236
|
+ if (!regName.test(name)) return false;
|
|
237
|
+ return true;
|
|
238
|
+}
|
|
239
|
+/**
|
|
240
|
+ * 判断是否为整数
|
|
241
|
+ */
|
|
242
|
+export function validatenum(num, type) {
|
|
243
|
+ let regName = /[^\d.]/g;
|
|
244
|
+ if (type === 1) {
|
|
245
|
+ if (!regName.test(num)) return false;
|
|
246
|
+ } else if (type === 2) {
|
|
247
|
+ regName = /[^\d]/g;
|
|
248
|
+ if (!regName.test(num)) return false;
|
|
249
|
+ }
|
|
250
|
+ return true;
|
|
251
|
+}
|
|
252
|
+/**
|
|
253
|
+ * 判断是否为小数
|
|
254
|
+ */
|
|
255
|
+export function validatenumord(num, type) {
|
|
256
|
+ let regName = /[^\d.]/g;
|
|
257
|
+ if (type === 1) {
|
|
258
|
+ if (!regName.test(num)) return false;
|
|
259
|
+ } else if (type === 2) {
|
|
260
|
+ regName = /[^\d.]/g;
|
|
261
|
+ if (!regName.test(num)) return false;
|
|
262
|
+ }
|
|
263
|
+ return true;
|
|
264
|
+}
|
|
265
|
+/**
|
|
266
|
+ * 判断是否为空
|
|
267
|
+ */
|
|
268
|
+export function validatenull(val) {
|
|
269
|
+ if (typeof val === "boolean") {
|
|
270
|
+ return false;
|
|
271
|
+ }
|
|
272
|
+ if (typeof val === "number") {
|
|
273
|
+ return false;
|
|
274
|
+ }
|
|
275
|
+ if (val instanceof Array) {
|
|
276
|
+ if (val.length === 0) return true;
|
|
277
|
+ } else if (val instanceof Object) {
|
|
278
|
+ if (JSON.stringify(val) === "{}") return true;
|
|
279
|
+ } else {
|
|
280
|
+ if (
|
|
281
|
+ val === "null" ||
|
|
282
|
+ val === null ||
|
|
283
|
+ val === "undefined" ||
|
|
284
|
+ val === undefined ||
|
|
285
|
+ val === ""
|
|
286
|
+ )
|
|
287
|
+ return true;
|
|
288
|
+ return false;
|
|
289
|
+ }
|
|
290
|
+ return false;
|
|
291
|
+}
|
|
292
|
+// 密码必须是6-16位的英文字母、数字组合
|
|
293
|
+export function isLoginPwd(val) {
|
|
294
|
+ const passRegex = new RegExp("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
|
|
295
|
+ return passRegex.test(val);
|
|
296
|
+}
|
|
297
|
+// 修改密码功能改为: 密码必须是8-16位大小写英文字母、数字组合 。
|
|
298
|
+export function isValidPassword(password) {
|
|
299
|
+ const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,16}$/;
|
|
300
|
+ return {
|
|
301
|
+ is: reg.test(password),
|
|
302
|
+ msg: "密码必须是8-16位大小写英文字母、数字组合"
|
|
303
|
+ };
|
|
304
|
+}
|