-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
262 lines (236 loc) · 8.51 KB
/
validate.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
define(function() {
'use strict';
var check,
test = RegExp.prototype.test,
tests = {
"Generic":{
test : test.bind(/^[^<>]+$/),
message : "This field may not contain less than signs (<) or greater than signs (>)"
},
"AlphaNumeric":{
test : test.bind(/^[0-9A-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F]+$/),
message : "This field must contain only alphanumeric characters"
},
"Alpha":{
test : test.bind(/^[A-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F]+$/),
message : "This field must contain only alpha characters"
},
"AlphaDash":{
test : test.bind(/^[A-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F\-]+$/),
message : "This field must contain only alpha characters or dashes"
},
"ANDash":{
test : test.bind(/^[0-9A-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F\-]+$/),
message : "This field must contain only alphanumeric characters or dashes"
},
"ANUnder":{
test : test.bind(/^[0-9A-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F_]+$/),
message : "This field must contain only alphanumeric characters with or without underscores"
},
"ANUSpace":{
test : test.bind(/^[0-9A-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F_ ]+$/),
message : "This field must contain only alphanumeric characters with or without underscores and spaces"
},
"ANEmail":{
test : test.bind(/^([_\dA-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F\-]+|[\w\.\+\-]+@(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))$/),
message : "This field must contain a valid username or email"
},
"Integer":{
test : test.bind(/^\-?\d+$/),
message : "This field must only contain numbers, without any spaces"
},
"CreditCardNumber":{
test : test.bind(/^\d{13,16}$/),
message : "This field must only contain numbers, without any spaces or dashes"
},
"Decimal":{
test : test.bind(/^\-?\d+(\.\d+)?$/),
message : "This field must be a valid decimal number"
},
"Date":{
test : test.bind(/^\d{1,2}\-\d{1,2}-\d{4}( \d{2}:\d{2}:\d{2})?$/),
message : "This field must be a valid date"
},
"SqlDate":{
test : test.bind(/^\d{4}\-\d{2}\-\d{2}$/),
message : "This field must be a valid date"
},
"SqlDateTime":{
test : test.bind(/^\d{4}\-\d{2}\-\d{2}\s\d{2}\:\d{2}\:\d{2}$/),
message : "This field must be a valid datetime"
},
"SlashDate":{
test : test.bind(/^\d{1,2}\/\d{1,2}\/\d{4}$/),
message : "This field must be a valid date"
},
"Email":{
test : test.bind(/^[\w\.\+\-]+@(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/),
message : "This field must be a valid email address"
},
"Name":{
test : test.bind(/^[\wA-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F\'. \-]{2,50}$/),
message : "This field must be a valid name"
},
"Username":{
test : test.bind(/^[A-Za-z0-9_\-]+$/),
message : "This field contains invalid characters. Please use only letters, numbers, dash or underscore characters."
},
"Password":{
test : test.bind(/^\S{6,32}$/),
message : "This field must be between 6 and 32 characters"
},
"Address":{
test : test.bind(/^[\w0-9A-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F# \' \.\,\&\-]+$/),
message : "This field must be a valid address"
},
"City":{
test : test.bind(/^[\wA-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F \' \. \-]+$/),
message : "This field must be a valid city"
},
"Province":{
test : test.bind(/^[\wA-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F ]+$/),
message : "This field must be a valid province"
},
"IntZip":{
test : test.bind(/^[A-Za-z0-9#\. \-]+$/),
message : "This field must be a valid zipcode"
},
"UsZip":{
test : test.bind(/^\d{5}(\-\d{4})?$/),
message : "This field must be a valid US zipcode"
},
"Country":{
test : test.bind(/^[\wA-Za-z\u00C0-\u00FF\u0100-\u0259\u0386\u0388-\u04E9\u05D0-\u06D3\u1E80-\u200F\'. \-]{2,50}$/),
message : "This field must be a valid country"
},
"IntPhone":{
test : test.bind(/^[0-9\+ \(\)\#\-]+$/),
message : "This field must be a valid phone"
},
"UsPhone":{
test : test.bind(/^\d{3}\-\d{3}\-\d{4}$/),
message : "This field must be a valid US phone"
},
"PicExt":{
test : test.bind(/^((jpg)|(jpeg)|(png)|(gif)){1}$/),
message : "This field must be a valid image extension"
},
"VideoExt":{
test : test.bind(/^((mpg)|(mpeg)|(mov)|(avi)|(dv)|(qt)|(asf)|(flv)){1}$/),
message : "This field must be a valid video extension"
},
"Url":{
test : test.bind(/^(http(?:s)?:\/\/|www.)[^<>]*$/),
message : "This field must be a URL starting with http:// or www."
},
"UrlExt":{
test : test.bind(/^((?:https?):\/\/)?(?:(?:(?:[\w\.\-\+!$&\'\(\)*\+,;=_]|%[0-9a-f]{2})+:)*(?:[\w\.\-\+%!$&\'\(\)*\+,;=]|%[0-9a-f]{2})+@)?(?:[A-Za-z0-9_\-]+\.)(?:[A-Za-z0-9\-\._])+(?::\d+)?(?:[\/|\?](?:[\w#!:\.\?\+=&@$\'~*,;_\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/),
message : "This field must be a valid URL"
},
"Html":{
test : function() {
return !test.apply(/<((?!\/?span|\/?h1|\/?h2|\/?h3|\/?h4|\/?h5|\/?h6|\/?a|\/?b|\/?ol|\/?ul|\/?li|\/?i|\/?em(?!bed)|\/?p|\/?div|\/?br|\/?unb|\/?uni|\/?\s|\/?\>)[^\>]*\>)/i, arguments);
},
message : "This field must be properly formed HTML"
},
"Twitter":{
test : test.bind(/^[A-Za-z0-9_\-]{1,15}$/),
message : "This field must be a valid twitter username (without the @ character)"
},
"required":{
test : test.bind(/.+/),
message : "This field is required"
},
"length":{
test : function(corpus, meta) {
var limits = /\[(,?\d+(?:,\d+)?)\]/.exec(meta);
corpus = String(corpus).replace(/[\s]+/g, ' ');
if (!limits) { return false; }
return (new RegExp('^.{'+limits[1]+'}$')).test( corpus );
},
message : function(corpus, meta) {
var limits;
limits = /\[(\d+),(\d+)\]/.exec(meta);
if (limits) {
return "Must be between "+limits[1]+" and "+limits[2]+" characters.";
}
limits = /\[,(\d+)\]/.exec(meta);
if (limits) {
return "Must be at most "+limits[1]+" characters.";
}
limits = /\[(\d+),\]/.exec(meta);
if (limits) {
return "Must be at least "+limits[1]+" characters.";
}
limits = /\[(\d+)\]/.exec(meta);
if (limits) {
return "Must be exactly "+limits[1]+" characters.";
}
}
}
},
// For extracting metada e.g. length[x,y]
metaRule = /(\w+)(.*)/;
function commaSplit(str) {
var i, buffer,
ignore = false,
start = 0,
result = [];
for (i=0; i < str.length; ++i) {
if (str[i]==='[') {
ignore = true;
continue;
}
if (str[i]===']') {
ignore = false;
continue;
}
if (str[i]===',' && !ignore) {
buffer = str.substring(start, i);
if ( buffer ) {
result.push(buffer);
}
start = i+1;
}
}
buffer = str.substring(start);
if ( buffer ) {
result.push(buffer);
}
return result;
}
function validate( body, rules ) {
var optional;
delete validate.message;
rules = rules ? commaSplit(rules) : [];
body = (body === null || body === undefined) ? '' : body;
optional = rules.indexOf('optional');
if (optional > -1) {
rules.splice(optional, optional + 1);
return !body || rules.every(check, body);
}
else {
return rules.every(check, body);
}
}
// Per rule check
check = function (rule) {
var res = metaRule.exec(rule),
meta;
if (res) {
rule = res[1];
meta = res[2];
}
if (!(tests[rule] && tests[rule].test)) {
return true;
}
res = tests[rule].test(this, meta);
if (!res) {
validate.message = typeof tests[rule].message === 'function' ?
tests[rule].message(this, meta) :
tests[rule].message;
}
return res;
};
return validate;
});