-
Notifications
You must be signed in to change notification settings - Fork 3
/
validator.js
254 lines (234 loc) · 6.77 KB
/
validator.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.toDate = toDate;
exports.isDate = isDate;
exports.isBefore = isBefore;
exports.isAfter = isAfter;
exports.isIn = isIn;
exports.isEmail = isEmail;
exports.isJSON = isJSON;
exports.isIP = isIP;
exports.isURL = isURL;
exports.isFQDN = isFQDN;
exports.isCreditCard = isCreditCard;
var regx = {
phone: /^(\+?0?86\-?)?1[3456789]\d{9}$/,
// 手机号段正则表达式 (2019-01 最新) https://blog.csdn.net/u011415782/article/details/85601655
phoneStrict: /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/,
email: /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i,
creditCard: /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/,
objectId: /^[0-9a-fA-F]{24}$/,
specialChars: /[#'$%^&*\/\\\|<>\[\]{}]/g,
alpha: /^[A-Z]+$/i,
alphanumeric: /^[0-9A-Z]+$/i,
numeric: /^[-+]?[0-9\.]+$/,
int: /^(?:[-+]?(?:0|[1-9][0-9]*))$/,
float: /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/,
hexadecimal: /^[0-9A-F]+$/i,
decimal: /^[-+]?([0-9]+|\.[0-9]+|[0-9]+\.[0-9]+)$/,
hexcolor: /^#?([0-9A-F]{3}|[0-9A-F]{6})$/i,
ascii: /^[\x00-\x7F]+$/,
base64: /^(?:[A-Z0-9+\/]{4})*(?:[A-Z0-9+\/]{2}==|[A-Z0-9+\/]{3}=|[A-Z0-9+\/]{4})$/i,
ipv4Maybe: /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/,
ipv6Block: /^[0-9A-F]{1,4}$/i
};
exports.regx = regx;
function merge(obj, defaults) {
obj = obj || {};
for (var key in defaults) {
if (typeof obj[key] === 'undefined') {
obj[key] = defaults[key];
}
}
return obj;
}
function toString(input) {
if (typeof input === 'object' && input !== null && input.toString) {
input = input.toString();
} else if (input === null || typeof input === 'undefined' || isNaN(input) && !input.length) {
input = '';
} else if (typeof input !== 'string') {
input += '';
}
return input;
};
function toDate(date) {
if (Object.prototype.toString.call(date) === '[object Date]') {
return date;
}
date = Date.parse(date);
return !isNaN(date) ? new Date(date) : null;
}
function isDate(str) {
return !isNaN(Date.parse(str));
}
function isBefore(str, date) {
var comparison = toDate(date || new Date()),
original = toDate(str);
return !!(original && comparison && original < comparison);
}
function isAfter(str, date) {
var comparison = toDate(date || new Date()),
original = toDate(str);
return !!(original && comparison && original > comparison);
}
function isIn(str, options) {
var i;
if (Object.prototype.toString.call(options) === '[object Array]') {
var array = [];
for (i in options) {
array[i] = toString(options[i]);
}
return array.indexOf(str) >= 0;
} else if (typeof options === 'object') {
return options.hasOwnProperty(str);
} else if (options && typeof options.indexOf === 'function') {
return options.indexOf(str) >= 0;
}
return false;
}
function isEmail(str) {
return regx.email.test(str);
}
function isJSON() {
try {
var obj = JSON.parse(str);
return !!obj && typeof obj === 'object';
} catch (e) {}
return false;
}
function isIP(str) {
return regx.ipv4Maybe.test(str) || regx.ipv6Block.test(str);
}
var default_url_options = {
protocols: ['http', 'https', 'ftp'],
require_tld: true,
require_protocol: false,
require_valid_protocol: true,
allow_underscores: false,
allow_trailing_dot: false,
allow_protocol_relative_urls: false
};
function isURL(url, options) {
if (!url || url.length >= 2083 || /\s/.test(url)) {
return false;
}
if (url.indexOf('mailto:') === 0) {
return false;
}
options = merge(options, default_url_options);
var protocol, auth, host, hostname, port, port_str, split;
split = url.split('://');
if (split.length > 1) {
protocol = split.shift();
if (options.require_valid_protocol && options.protocols.indexOf(protocol) === -1) {
return false;
}
} else if (options.require_protocol) {
return false;
} else if (options.allow_protocol_relative_urls && url.substr(0, 2) === '//') {
split[0] = url.substr(2);
}
url = split.join('://');
split = url.split('#');
url = split.shift();
split = url.split('?');
url = split.shift();
split = url.split('/');
url = split.shift();
split = url.split('@');
if (split.length > 1) {
auth = split.shift();
if (auth.indexOf(':') >= 0 && auth.split(':').length > 2) {
return false;
}
}
hostname = split.join('@');
split = hostname.split(':');
host = split.shift();
if (split.length) {
port_str = split.join(':');
port = parseInt(port_str, 10);
if (!/^[0-9]+$/.test(port_str) || port <= 0 || port > 65535) {
return false;
}
}
if (!isIP(host) && !isFQDN(host, options) && host !== 'localhost') {
return false;
}
if (options.host_whitelist && options.host_whitelist.indexOf(host) === -1) {
return false;
}
if (options.host_blacklist && options.host_blacklist.indexOf(host) !== -1) {
return false;
}
return true;
}
var default_fqdn_options = {
require_tld: true,
allow_underscores: false,
allow_trailing_dot: false
};
function isFQDN(str, options) {
options = merge(options, default_fqdn_options);
/* Remove the optional trailing dot before checking validity */
if (options.allow_trailing_dot && str[str.length - 1] === '.') {
str = str.substring(0, str.length - 1);
}
var parts = str.split('.');
if (options.require_tld) {
var tld = parts.pop();
if (!parts.length || !/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
return false;
}
}
for (var part, i = 0; i < parts.length; i++) {
part = parts[i];
if (options.allow_underscores) {
if (part.indexOf('__') >= 0) {
return false;
}
part = part.replace(/_/g, '');
}
if (!/^[a-z\u00a1-\uffff0-9-]+$/i.test(part)) {
return false;
}
if (/[\uff01-\uff5e]/.test(part)) {
// disallow full-width chars
return false;
}
if (part[0] === '-' || part[part.length - 1] === '-' || part.indexOf('---') >= 0) {
return false;
}
}
return true;
}
;
function isCreditCard(str) {
var sanitized = str.replace(/[^0-9]+/g, '');
if (!regx.creditCard.test(sanitized)) {
return false;
}
var sum = 0,
digit,
tmpNum,
shouldDouble;
for (var i = sanitized.length - 1; i >= 0; i--) {
digit = sanitized.substring(i, i + 1);
tmpNum = parseInt(digit, 10);
if (shouldDouble) {
tmpNum *= 2;
if (tmpNum >= 10) {
sum += tmpNum % 10 + 1;
} else {
sum += tmpNum;
}
} else {
sum += tmpNum;
}
shouldDouble = !shouldDouble;
}
return !!(sum % 10 === 0 ? sanitized : false);
}