Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.validate.check");
12
dojo.require("dojo.validate.common");
13
dojo.require("dojo.lang.common");
14
dojo.validate.check = function (form, profile) {
15
	var missing = [];
16
	var invalid = [];
17
	var results = {isSuccessful:function () {
18
		return (!this.hasInvalid() && !this.hasMissing());
19
	}, hasMissing:function () {
20
		return (missing.length > 0);
21
	}, getMissing:function () {
22
		return missing;
23
	}, isMissing:function (elemname) {
24
		for (var i = 0; i < missing.length; i++) {
25
			if (elemname == missing[i]) {
26
				return true;
27
			}
28
		}
29
		return false;
30
	}, hasInvalid:function () {
31
		return (invalid.length > 0);
32
	}, getInvalid:function () {
33
		return invalid;
34
	}, isInvalid:function (elemname) {
35
		for (var i = 0; i < invalid.length; i++) {
36
			if (elemname == invalid[i]) {
37
				return true;
38
			}
39
		}
40
		return false;
41
	}};
42
	if (profile.trim instanceof Array) {
43
		for (var i = 0; i < profile.trim.length; i++) {
44
			var elem = form[profile.trim[i]];
45
			if (dj_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
46
				continue;
47
			}
48
			elem.value = elem.value.replace(/(^\s*|\s*$)/g, "");
49
		}
50
	}
51
	if (profile.uppercase instanceof Array) {
52
		for (var i = 0; i < profile.uppercase.length; i++) {
53
			var elem = form[profile.uppercase[i]];
54
			if (dj_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
55
				continue;
56
			}
57
			elem.value = elem.value.toUpperCase();
58
		}
59
	}
60
	if (profile.lowercase instanceof Array) {
61
		for (var i = 0; i < profile.lowercase.length; i++) {
62
			var elem = form[profile.lowercase[i]];
63
			if (dj_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
64
				continue;
65
			}
66
			elem.value = elem.value.toLowerCase();
67
		}
68
	}
69
	if (profile.ucfirst instanceof Array) {
70
		for (var i = 0; i < profile.ucfirst.length; i++) {
71
			var elem = form[profile.ucfirst[i]];
72
			if (dj_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
73
				continue;
74
			}
75
			elem.value = elem.value.replace(/\b\w+\b/g, function (word) {
76
				return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
77
			});
78
		}
79
	}
80
	if (profile.digit instanceof Array) {
81
		for (var i = 0; i < profile.digit.length; i++) {
82
			var elem = form[profile.digit[i]];
83
			if (dj_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
84
				continue;
85
			}
86
			elem.value = elem.value.replace(/\D/g, "");
87
		}
88
	}
89
	if (profile.required instanceof Array) {
90
		for (var i = 0; i < profile.required.length; i++) {
91
			if (!dojo.lang.isString(profile.required[i])) {
92
				continue;
93
			}
94
			var elem = form[profile.required[i]];
95
			if (!dj_undef("type", elem) && (elem.type == "text" || elem.type == "textarea" || elem.type == "password") && /^\s*$/.test(elem.value)) {
96
				missing[missing.length] = elem.name;
97
			} else {
98
				if (!dj_undef("type", elem) && (elem.type == "select-one" || elem.type == "select-multiple") && (elem.selectedIndex == -1 || /^\s*$/.test(elem.options[elem.selectedIndex].value))) {
99
					missing[missing.length] = elem.name;
100
				} else {
101
					if (elem instanceof Array) {
102
						var checked = false;
103
						for (var j = 0; j < elem.length; j++) {
104
							if (elem[j].checked) {
105
								checked = true;
106
							}
107
						}
108
						if (!checked) {
109
							missing[missing.length] = elem[0].name;
110
						}
111
					}
112
				}
113
			}
114
		}
115
	}
116
	if (profile.required instanceof Array) {
117
		for (var i = 0; i < profile.required.length; i++) {
118
			if (!dojo.lang.isObject(profile.required[i])) {
119
				continue;
120
			}
121
			var elem, numRequired;
122
			for (var name in profile.required[i]) {
123
				elem = form[name];
124
				numRequired = profile.required[i][name];
125
			}
126
			if (elem instanceof Array) {
127
				var checked = 0;
128
				for (var j = 0; j < elem.length; j++) {
129
					if (elem[j].checked) {
130
						checked++;
131
					}
132
				}
133
				if (checked < numRequired) {
134
					missing[missing.length] = elem[0].name;
135
				}
136
			} else {
137
				if (!dj_undef("type", elem) && elem.type == "select-multiple") {
138
					var selected = 0;
139
					for (var j = 0; j < elem.options.length; j++) {
140
						if (elem.options[j].selected && !/^\s*$/.test(elem.options[j].value)) {
141
							selected++;
142
						}
143
					}
144
					if (selected < numRequired) {
145
						missing[missing.length] = elem.name;
146
					}
147
				}
148
			}
149
		}
150
	}
151
	if (dojo.lang.isObject(profile.dependencies) || dojo.lang.isObject(profile.dependancies)) {
152
		if (profile["dependancies"]) {
153
			dojo.deprecated("dojo.validate.check", "profile 'dependancies' is deprecated, please use " + "'dependencies'", "0.5");
154
			profile.dependencies = profile.dependancies;
155
		}
156
		for (name in profile.dependencies) {
157
			var elem = form[name];
158
			if (dj_undef("type", elem)) {
159
				continue;
160
			}
161
			if (elem.type != "text" && elem.type != "textarea" && elem.type != "password") {
162
				continue;
163
			}
164
			if (/\S+/.test(elem.value)) {
165
				continue;
166
			}
167
			if (results.isMissing(elem.name)) {
168
				continue;
169
			}
170
			var target = form[profile.dependencies[name]];
171
			if (target.type != "text" && target.type != "textarea" && target.type != "password") {
172
				continue;
173
			}
174
			if (/^\s*$/.test(target.value)) {
175
				continue;
176
			}
177
			missing[missing.length] = elem.name;
178
		}
179
	}
180
	if (dojo.lang.isObject(profile.constraints)) {
181
		for (name in profile.constraints) {
182
			var elem = form[name];
183
			if (!elem) {
184
				continue;
185
			}
186
			if (!dj_undef("tagName", elem) && (elem.tagName.toLowerCase().indexOf("input") >= 0 || elem.tagName.toLowerCase().indexOf("textarea") >= 0) && /^\s*$/.test(elem.value)) {
187
				continue;
188
			}
189
			var isValid = true;
190
			if (dojo.lang.isFunction(profile.constraints[name])) {
191
				isValid = profile.constraints[name](elem.value);
192
			} else {
193
				if (dojo.lang.isArray(profile.constraints[name])) {
194
					if (dojo.lang.isArray(profile.constraints[name][0])) {
195
						for (var i = 0; i < profile.constraints[name].length; i++) {
196
							isValid = dojo.validate.evaluateConstraint(profile, profile.constraints[name][i], name, elem);
197
							if (!isValid) {
198
								break;
199
							}
200
						}
201
					} else {
202
						isValid = dojo.validate.evaluateConstraint(profile, profile.constraints[name], name, elem);
203
					}
204
				}
205
			}
206
			if (!isValid) {
207
				invalid[invalid.length] = elem.name;
208
			}
209
		}
210
	}
211
	if (dojo.lang.isObject(profile.confirm)) {
212
		for (name in profile.confirm) {
213
			var elem = form[name];
214
			var target = form[profile.confirm[name]];
215
			if (dj_undef("type", elem) || dj_undef("type", target) || (elem.type != "text" && elem.type != "textarea" && elem.type != "password") || (target.type != elem.type) || (target.value == elem.value) || (results.isInvalid(elem.name)) || (/^\s*$/.test(target.value))) {
216
				continue;
217
			}
218
			invalid[invalid.length] = elem.name;
219
		}
220
	}
221
	return results;
222
};
223
dojo.validate.evaluateConstraint = function (profile, constraint, fieldName, elem) {
224
	var isValidSomething = constraint[0];
225
	var params = constraint.slice(1);
226
	params.unshift(elem.value);
227
	if (typeof isValidSomething != "undefined") {
228
		return isValidSomething.apply(null, params);
229
	}
230
	return false;
231
};
232