深度优先

这个家伙好懒,除了文章什么都没留下

0%

【JS】常用的验证类

今天开始准备有时间,看一些开源项目,学习那些大牛们的编码风格。

第一个项目:《MVC5仓库管理系统后台管理源码》

该程序使用的.NET MVC开发,大量使用到了jQuery,以及Bootstrap。 数据库使用了SQL Server.

很适合现阶段看,大部分代码还算看得懂。

然后刚刚看到一个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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
var git = {
//true表示邮箱格式正确
checkEmail: function (str) {
if (str.match(/[A-Za-z0-9_-]+[@](\S*)(net|com|cn|org|cc|tv|[0-9]{1,3})(\S*)/g) == null) {
return false;
}
else {
return true;
}
},
//true表示手机号格式正确
checkMobilePhone: function (str) {
if (str.match(/^(?:13\d|15[0-9]|18[0123456789])-?\d{5}(\d{3}|\*{3})$/) == null) {
return false;
}
else {
return true;
}
},
//true表示为数字
checkNum: function (str) {
if (str.match(/\D/) == null) {
return false;
}
else {
return true;
}
},
//true表示为小数
checkDecimal: function (str) {
if (str.match(/^-?\d+(\.\d+)?$/g) == null) {
return false;
}
else {
return true;
}
},
//true表示为小数
checkInteger: function (str) {
if (str.match(/^[-+]?\d*$/) == null) {
return false;
}
else {
return true;
}
},
//true表示为全部为字符 不包含汉字
checkStr: function (str) {
if (/[^\x00-\xff]/g.test(str)) {
return false;
}
else {
return true;
}
},
//true表示包含汉字
checkChinese: function (str) {
if (escape(str).indexOf("%u") != -1) {
return true;
}
else {
return false;
}
},
//true表示格式正确
checkTelephone: function (str) {
if (str.match(/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/) == null) {
return false;
}
else {
return true;
}
},
//true表示格式正确 检查输入的身份证号是否正确
checkCard: function (str) {
//15位数身份证正则表达式
var arg1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
//18位数身份证正则表达式
var arg2 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$/;
if (str.match(arg1) == null && str.match(arg2) == null) {
return false;
}
else {
return true;
}
},
//检查输入的IP地址是否正确 true表示格式正确
checkIP: function (str) {
var arg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
if (str.match(arg) == null) {
return false;
}
else {
return true;
}
},
//检查输入的URL地址是否正确 true表示格式正确
checkURL: function (str) {
//if (str.match(/(http[s]?|ftp):\/\/[^\/\.]+?\..+\w$/i) == null) {
if (str.match(/(http[s]?|ftp):\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/) == null) {
return false
}
else {
return true;
}
},
//检查字符串是否为空
IsEmpty: function (str) {
if (str == undefined || str == "") {
return true;
} else {
return false;
}
},
//域名验证
IsURL: function (URL) {
var str = URL;
var Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
var objExp = new RegExp(Expression);
if (objExp.test(str) == true) {
return true;
} else {
return false;
}
},
//true只有数字和字母和下划线
IsNumOrLet: function (parVal) {
if (parVal.match(/[^a-zA-Z0-9_]+/)) {
return true;
} else {
return false;
}
},
//验证Money,true表示正确
checkMoney: function (str) {
if (str.match(/^[0-9][\d]{0,11}([\.][\d]{0,4})?$/)) {
return true;
} else {
return false;
}
},
//true表示开始时间大于或等于结束时间
CompareTime: function (stime, etime) {
var starttimes = stime.split('-');
var endtimes = etime.split('-');
var starttimeTemp = starttimes[0] + '/' + starttimes[1] + '/' + starttimes[2];
var endtimesTemp = endtimes[0] + '/' + endtimes[1] + '/' + endtimes[2];
if (Date.parse(new Date(starttimeTemp)) > Date.parse(new Date(endtimesTemp)) || Date.parse(new Date(starttimeTemp)) == Date.parse(new Date(endtimesTemp))) {
return true;
}
},
//true表示开始时间大于或等于结束时间,只有年月日
checkCompareTime: function (stime, etime) {
var starttimes = stime.split('-');
var endtimes = etime.split('-');
var starttimeTemp = starttimes[0] + '/' + starttimes[1] + '/' + starttimes[2];
var endtimesTemp = endtimes[0] + '/' + endtimes[1] + '/' + endtimes[2];
if (Date.parse(new Date(starttimeTemp)) > Date.parse(new Date(endtimesTemp))) {
return true;
}
},
//??
Escape: function (value) {
if (value != undefined) {
return escape(value);
}
return undefined;
},
//递归啥??
UnEscape: function (value) {
if (value != undefined) {
return unescape(value);
}
return undefined;
},
//.net Json后的日期转换
JsonToDateTime: function (jsonDate) {
if (jsonDate == undefined || jsonDate.length <= 0) return jsonDate;
var date = new Date(parseInt(jsonDate.substr(6))).format("yyyy-MM-dd HH:mm:ss");
return date;
},
//.net Json后的日期转换 只有年月日
JsonToDateTimeymd: function (jsonDate) {
if (jsonDate == undefined || jsonDate.length <= 0) return jsonDate;
var date = new Date(parseInt(jsonDate.substr(6))).format("yyyy-MM-dd");
return date;
},
//获得访问的路径/Home/Index
GetPath: function () {
return window.location.pathname;
},
//获得Url地址
GetUrl: function () {
return window.location.href;
},
//获得访问协议 http https
GetProtocol: function () {
return window.location.protocol;
},
//获取域名
GetDomain: function () {
return document.domain;
},
//获得端口
GetPort: function () {
return window.location.port;
},
GetArgs: function () {
var args = {};
var query = location.search.substring(1);
var pairs = query.split("&");
for (var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexOf('=');
if (pos == -1) {
continue;
} else {
var argname = pairs[i].substring(0, pos);
var value = pairs[i].substring(pos + 1);
value = decodeURIComponent(value);
args[argname] = value;
}
}
return args;
},
//去所有空格
Trim: function (value) {
if (value == undefined || value == "") {
return value;
}
value = value.replace(/[ ]/g, "");
return value;
},
//去左边的空格
LTrim: function (value) {
if (value == undefined || value == "") {
return value;
}
value = value.replace(/(^\s*)/g, "");
return value;
},
//去右边空格
RTrim: function (value) {
if (value == undefined || value == "") {
return value;
}
value = value.replace(/(\s*$)/g, "");
return value;
},
//转换成小数,x:转换的对象,y:小数点后面的位数
ToDecimal: function (x, y) {
var f_x = parseFloat(x);
if (isNaN(f_x)) {

return false;
}
var f_x = Math.round(x * 100000) / 100000;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + y) {
s_x += '0';
}
return s_x;
},
//在键值对中,根据值获取建的信息
GetEnumDesc: function (item, value) {
if (item != undefined) {
var returnValue = "";
for (var i = 0; i < item.length; i++) {
var json = item[i];
if (json.Value == value) {
returnValue = json.Description;
break;
}
}
return returnValue;
}
return "";
},
//截取字符串
GetStrSub: function (str,lth)
{
var resultStr = "";
if (str == undefined || str == "") {
resultStr = "";
}
else {
if (str.length > lth) {
resultStr = str.substring(0, lth) + '...';
}
else {
resultStr = str;
}
}
return resultStr;
}
};

/*
函数:格式化日期
参数:formatStr-格式化字符串
d:将日显示为不带前导零的数字,如1
dd:将日显示为带前导零的数字,如01
ddd:将日显示为缩写形式,如Sun
dddd:将日显示为全名,如Sunday
M:将月份显示为不带前导零的数字,如一月显示为1
MM:将月份显示为带前导零的数字,如01
MMM:将月份显示为缩写形式,如Jan
MMMM:将月份显示为完整月份名,如January
yy:以两位数字格式显示年份
yyyy:以四位数字格式显示年份
h:使用12小时制将小时显示为不带前导零的数字,注意||的用法
hh:使用12小时制将小时显示为带前导零的数字
H:使用24小时制将小时显示为不带前导零的数字
HH:使用24小时制将小时显示为带前导零的数字
m:将分钟显示为不带前导零的数字
mm:将分钟显示为带前导零的数字
s:将秒显示为不带前导零的数字
ss:将秒显示为带前导零的数字
l:将毫秒显示为不带前导零的数字
ll:将毫秒显示为带前导零的数字
tt:显示am/pm
TT:显示AM/PM
返回:格式化后的日期
*/
Date.prototype.format = function (formatStr) {
var date = this;
/*
函数:填充0字符
参数:value-需要填充的字符串, length-总长度
返回:填充后的字符串
*/
var zeroize = function (value, length) {
if (!length) {
length = 2;
}
value = new String(value);
for (var i = 0, zeros = ''; i < (length - value.length) ; i++) {
zeros += '0';
}
return zeros + value;
};
return formatStr.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|M{1,4}|yy(?:yy)?|([hHmstT])\1?|[lLZ])\b/g, function ($0) {
switch ($0) {
case 'd': return date.getDate();
case 'dd': return zeroize(date.getDate());
case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][date.getDay()];
case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
case 'M': return date.getMonth() + 1;
case 'MM': return zeroize(date.getMonth() + 1);
case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][date.getMonth()];
case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][date.getMonth()];
case 'yy': return new String(date.getFullYear()).substr(2);
case 'yyyy': return date.getFullYear();
case 'h': return date.getHours() % 12 || 12;
case 'hh': return zeroize(date.getHours() % 12 || 12);
case 'H': return date.getHours();
case 'HH': return zeroize(date.getHours());
case 'm': return date.getMinutes();
case 'mm': return zeroize(date.getMinutes());
case 's': return date.getSeconds();
case 'ss': return zeroize(date.getSeconds());
case 'l': return date.getMilliseconds();
case 'll': return zeroize(date.getMilliseconds());
case 'tt': return date.getHours() < 12 ? 'am' : 'pm';
case 'TT': return date.getHours() < 12 ? 'AM' : 'PM';
}
});
}