1 | function Validator(frm) { |
---|
2 | this.form = frm; |
---|
3 | this.fields = {}; |
---|
4 | this.beforeSubmitHandler = null; |
---|
5 | Validator.registerValidator(this); |
---|
6 | } |
---|
7 | |
---|
8 | Validator.validators = []; |
---|
9 | |
---|
10 | OOUtils.extend(Validator, { |
---|
11 | doValidation: function() { |
---|
12 | for (var valInd in Validator.validators) { |
---|
13 | if (Validator.validators[valInd].form == this) { |
---|
14 | return Validator.validators[valInd].validate(); |
---|
15 | } |
---|
16 | } |
---|
17 | }, |
---|
18 | registerValidator: function(val) { |
---|
19 | val.form.onsubmit = Validator.doValidation; |
---|
20 | Validator.validators.push(val); |
---|
21 | } |
---|
22 | }); |
---|
23 | |
---|
24 | Validator.prototype = { |
---|
25 | validate: function() { |
---|
26 | for (var fieldName in this.fields) { |
---|
27 | var field = this.fields[fieldName].field; |
---|
28 | for (var valInd in this.fields[fieldName].validators) { |
---|
29 | if (!this.fields[fieldName].validators[valInd].validate(field)) { |
---|
30 | alert(FieldValidator.getFieldName(field) + " requires " + this.fields[fieldName].validators[valInd].requirement); |
---|
31 | field.select(); |
---|
32 | return false; |
---|
33 | } |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | if (this.beforeSubmitHandler) { |
---|
38 | return this.beforeSubmitHandler(this); |
---|
39 | } |
---|
40 | }, |
---|
41 | addFieldValidator: function(field, val) { |
---|
42 | if (!this.fields[field.id]) { |
---|
43 | this.fields[field.id] = {field: field, validators: []}; |
---|
44 | } |
---|
45 | this.fields[field.id].validators.push(val); |
---|
46 | }, |
---|
47 | setBeforeSubmitHandler: function(handler) { |
---|
48 | this.beforeSubmitHandler = handler; |
---|
49 | }, |
---|
50 | getField: function(fieldObj) { |
---|
51 | |
---|
52 | if (typeof(fieldObj) != "string") { |
---|
53 | fieldObj = fieldObj.id; |
---|
54 | } |
---|
55 | |
---|
56 | return this.fields[fieldObj]; |
---|
57 | }, |
---|
58 | getValidator: function(fieldObj, validatorType) { |
---|
59 | for (var validator in fieldObj.validators) { |
---|
60 | var currentValidator = fieldObj.validators[validator]; |
---|
61 | if (currentValidator instanceof validatorType) { |
---|
62 | return currentValidator; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | return null; |
---|
67 | } |
---|
68 | }; |
---|
69 | |
---|
70 | function FieldValidator() { } |
---|
71 | |
---|
72 | OOUtils.extend(FieldValidator, { |
---|
73 | getText: function(node) { |
---|
74 | |
---|
75 | if (node.title) { |
---|
76 | return node.title; |
---|
77 | } |
---|
78 | |
---|
79 | if (node.data) { |
---|
80 | return node.data; |
---|
81 | } |
---|
82 | |
---|
83 | var text = ""; |
---|
84 | for (var child in node.childNodes) { |
---|
85 | text += FieldValidator.getText(node.childNodes[child]); |
---|
86 | } |
---|
87 | |
---|
88 | return text; |
---|
89 | }, |
---|
90 | getFieldName: function(field) { |
---|
91 | var fieldName = "A field"; |
---|
92 | var labels = document.getElementsByTagName("label"); |
---|
93 | for (var labelInd in labels) { |
---|
94 | if (labels[labelInd].htmlFor == field.id) { |
---|
95 | fieldName = labels[labelInd].title || FieldValidator.getText(labels[labelInd]); |
---|
96 | break; |
---|
97 | } |
---|
98 | } |
---|
99 | return fieldName; |
---|
100 | } |
---|
101 | }); |
---|
102 | |
---|
103 | |
---|
104 | function REValidator() { } |
---|
105 | |
---|
106 | REValidator.prototype = { |
---|
107 | requirement: "any value", |
---|
108 | re: /.*/, |
---|
109 | validate: function(field) { |
---|
110 | var value = field.value; |
---|
111 | if (!this.re.test(value.trim())) { |
---|
112 | return false; |
---|
113 | } |
---|
114 | return true; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | function MandatoryValidator(childValidator) { |
---|
119 | if (childValidator) { |
---|
120 | this.requirement = childValidator.requirement; |
---|
121 | this.childValidator = childValidator; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | MandatoryValidator.prototype = new REValidator(); |
---|
126 | OOUtils.extend(MandatoryValidator.prototype, { |
---|
127 | requirement: "a value", |
---|
128 | re: /^.+$/, |
---|
129 | validate: function(field) { |
---|
130 | if (!REValidator.prototype.validate.apply(this, arguments)) { |
---|
131 | return false; |
---|
132 | } |
---|
133 | if (this.childValidator) { |
---|
134 | return this.childValidator.validate(field); |
---|
135 | } |
---|
136 | return true; |
---|
137 | } |
---|
138 | }); |
---|
139 | |
---|
140 | function IntegerValidator() { } |
---|
141 | |
---|
142 | IntegerValidator.prototype = new REValidator(); |
---|
143 | OOUtils.extend(IntegerValidator.prototype, { |
---|
144 | requirement: "an integer value", |
---|
145 | re: /^\-?\d*$/ |
---|
146 | }); |
---|
147 | |
---|
148 | function RealValidator() { } |
---|
149 | |
---|
150 | RealValidator.prototype = new REValidator(); |
---|
151 | OOUtils.extend(RealValidator.prototype, { |
---|
152 | requirement: "a real value", |
---|
153 | re: /^\d*(\.\d+)?$/ |
---|
154 | }); |
---|
155 | |
---|
156 | function DateValidator() { |
---|
157 | this.validatedDate = null; |
---|
158 | } |
---|
159 | |
---|
160 | DateValidator.prototype = new FieldValidator(); |
---|
161 | OOUtils.extend(DateValidator.prototype, { |
---|
162 | validate: function(field) { |
---|
163 | var dateObj; |
---|
164 | |
---|
165 | this.validatedDate = null; |
---|
166 | |
---|
167 | if (field.value.trim() == "") { |
---|
168 | return true; |
---|
169 | } |
---|
170 | |
---|
171 | try { |
---|
172 | dateObj = DateValidator.parse(field.value); |
---|
173 | } catch (e) { |
---|
174 | return false; |
---|
175 | } |
---|
176 | |
---|
177 | this.validatedDate = dateObj; |
---|
178 | return true; |
---|
179 | }, |
---|
180 | requirement: "a valid date" |
---|
181 | }); |
---|
182 | |
---|
183 | DateValidator.parse = function(dateStr) { |
---|
184 | var fullDateRE = /(\d+|\D{3,9})[\-\\\/\.](\d+|\D{3,9})[\-\\\/\.](\d+|\D{3,9})/; |
---|
185 | dateStr = dateStr.replace(/\s/g, ""); |
---|
186 | if (!re.test(dateStr)) |
---|
187 | throw "' " + dateStr + " ' is not a valid date string"; |
---|
188 | |
---|
189 | var foundMonth = 0; |
---|
190 | var foundYear = 0; |
---|
191 | var foundDay = 0; |
---|
192 | |
---|
193 | var matches = re.exec(dateStr); |
---|
194 | var monthTextRe = /\D{3,9}/; |
---|
195 | |
---|
196 | for (var x = 1; x <= 3; x++) { |
---|
197 | if (monthTextRe.test(matches[x])) { |
---|
198 | if (foundMonth != 0) |
---|
199 | throw "' " + dateStr + " ' is not a valid date string"; |
---|
200 | |
---|
201 | matches[x] = this.getMonthFromStr(matches[x]); |
---|
202 | |
---|
203 | if (!matches[x]) |
---|
204 | throw "' " + dateStr + " ' is not a valid date string"; |
---|
205 | |
---|
206 | foundMonth = x; |
---|
207 | } else { |
---|
208 | var num = new Number(matches[x]); |
---|
209 | if (num > 31 || num < 1) { |
---|
210 | if (foundYear != 0) |
---|
211 | throw "' " + dateStr + " ' is not a valid date string"; |
---|
212 | |
---|
213 | foundYear = x; |
---|
214 | } else { |
---|
215 | if (num > 12) { |
---|
216 | if (foundDay != 0) |
---|
217 | throw "' " + dateStr + " ' is not a valid date string"; |
---|
218 | |
---|
219 | foundDay = x; |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | if (foundYear != 0 && foundMonth != 0) { |
---|
226 | foundDay = (6 - (foundYear + foundMonth)); |
---|
227 | } else { |
---|
228 | if (foundYear != 0 && foundDay != 0) { |
---|
229 | foundMonth = (6 - (foundYear + foundDay)); |
---|
230 | } else { |
---|
231 | if (foundMonth != 0 && foundDay != 0) { |
---|
232 | foundYear = (6 - (foundMonth + foundDay)); |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | if (foundMonth == 0 && foundYear == 0 && foundDay == 0) { |
---|
238 | foundDay = 1; foundMonth = 2; foundYear = 3; |
---|
239 | } |
---|
240 | |
---|
241 | if (foundMonth == 0 || foundYear == 0 || foundDay == 0) { |
---|
242 | if (foundMonth != 0) { |
---|
243 | switch (foundMonth) { |
---|
244 | case 3 : throw "' " + dateStr + " ' is not a valid date string"; |
---|
245 | case 2 : foundDay = 1; foundYear = 3; break; |
---|
246 | case 1 : foundDay = 2; foundYear = 3; break; |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | if (foundYear != 0) { |
---|
251 | switch (foundYear) { |
---|
252 | case 3 : foundDay = 1; foundMonth = 2; break; |
---|
253 | case 2 : throw "' " + dateStr + " ' is not a valid date string"; |
---|
254 | case 1 : foundMonth = 2; foundDay = 3; break; |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | if (foundDay != 0) { |
---|
259 | switch (foundDay) { |
---|
260 | case 3 : foundYear = 1; foundMonth = 2; break; |
---|
261 | case 2 : throw "' " + dateStr + " ' is not a valid date string"; |
---|
262 | case 1 : foundMonth = 2; foundYear = 3; break; |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | var year, month, day; |
---|
268 | |
---|
269 | year = Number(matches[foundYear]); |
---|
270 | month = Number(matches[foundMonth]); |
---|
271 | day = Number(matches[foundDay]); |
---|
272 | |
---|
273 | var date = new Date(year, month - 1, day); |
---|
274 | return date; |
---|
275 | } |
---|
276 | |
---|
277 | DateValidator.getMonthFromStr = function(str) { |
---|
278 | var names = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]; |
---|
279 | |
---|
280 | for (var month in names) { |
---|
281 | if (str.toLowerCase() == names[month] || names[month].indexOf(str.toLowerCase()) == 0) { |
---|
282 | return new Number(month) + 1; |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | return false; |
---|
287 | } |
---|
288 | |
---|
289 | function TimeValidator() { |
---|
290 | this.validatedDate = null; |
---|
291 | } |
---|
292 | |
---|
293 | TimeValidator.prototype = new FieldValidator(); |
---|
294 | TimeValidator.prototype.requirement = "a valid time"; |
---|
295 | TimeValidator.prototype.validate = function(field) { |
---|
296 | var dateObj; |
---|
297 | |
---|
298 | this.validatedDate = null; |
---|
299 | |
---|
300 | if (field.value.trim() == "") { |
---|
301 | return true; |
---|
302 | } |
---|
303 | |
---|
304 | try { |
---|
305 | dateObj = TimeValidator.parse(field.value); |
---|
306 | } catch (e) { |
---|
307 | return false; |
---|
308 | } |
---|
309 | |
---|
310 | this.validatedDate = dateObj; |
---|
311 | return true; |
---|
312 | } |
---|
313 | |
---|
314 | TimeValidator.parse = function(timeStr) { |
---|
315 | var re = /^(\d{1,2})(:(\d{2}))?(:(\d{2}))?(AM|PM)?$/i; |
---|
316 | timeStr = timeStr.replace(/\s/g, ""); |
---|
317 | |
---|
318 | if (!re.test(timeStr)) { |
---|
319 | throw "'" + timeStr + "' is not a valid time string"; |
---|
320 | } |
---|
321 | |
---|
322 | var matches = re.exec(timeStr); |
---|
323 | |
---|
324 | var hours = Number(matches[1]); |
---|
325 | var minutes = 0; |
---|
326 | var seconds = 0; |
---|
327 | var noonRelation = null; |
---|
328 | |
---|
329 | if (matches[2] != null) { |
---|
330 | minutes = Number(matches[3]); |
---|
331 | } |
---|
332 | |
---|
333 | if (matches[4] != null) { |
---|
334 | seconds = Number(matches[5]); |
---|
335 | } |
---|
336 | |
---|
337 | if (matches[6] != null) { |
---|
338 | noonRelation = matches[6]; |
---|
339 | } |
---|
340 | |
---|
341 | if (noonRelation != null) { |
---|
342 | if (hours > 12 || hours < 1) { |
---|
343 | throw "'" + timeStr + "' is not a valid time string"; |
---|
344 | } |
---|
345 | |
---|
346 | hours %= 12; |
---|
347 | if (noonRelation.toLowerCase() == "pm") { |
---|
348 | hours += 12; |
---|
349 | } |
---|
350 | } |
---|
351 | |
---|
352 | if (minutes > 59 || seconds > 59 || hours > 23) { |
---|
353 | throw "'" + timeStr + "' is not a valid time string"; |
---|
354 | } |
---|
355 | |
---|
356 | var date = new Date(0); |
---|
357 | date.setSeconds(seconds); |
---|
358 | date.setMinutes(minutes); |
---|
359 | date.setHours(hours); |
---|
360 | |
---|
361 | return date; |
---|
362 | } |
---|
363 | |
---|
364 | function DateTimeValidator() { |
---|
365 | this.validateDate = null; |
---|
366 | } |
---|