Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojox.validate.check"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojox.validate.check"] = true;
3
dojo.provide("dojox.validate.check");
4
 
5
dojo.require("dojox.validate._base");
6
 
7
dojox.validate.check = function(/*HTMLFormElement*/form, /*Object*/profile){
8
	// summary: validates user input of an HTML form based on input profile
9
	//
10
	// description:
11
	//	returns an object that contains several methods summarizing the results of the validation
12
	//
13
	// form: form to be validated
14
	// profile: specifies how the form fields are to be validated
15
	// {trim:Array, uppercase:Array, lowercase:Array, ucfirst:Array, digit:Array,
16
	//	required:Array, dependencies:Object, constraints:Object, confirm:Object}
17
 
18
	// Essentially private properties of results object
19
	var missing = [];
20
	var invalid = [];
21
 
22
	// results object summarizes the validation
23
	var results = {
24
		isSuccessful: function() {return ( !this.hasInvalid() && !this.hasMissing() );},
25
		hasMissing: function() {return ( missing.length > 0 );},
26
		getMissing: function() {return missing;},
27
		isMissing: function(elemname) {
28
			for(var i = 0; i < missing.length; i++){
29
				if(elemname == missing[i]){ return true; }
30
			}
31
			return false;
32
		},
33
		hasInvalid: function() {return ( invalid.length > 0 );},
34
		getInvalid: function() {return invalid;},
35
		isInvalid: function(elemname){
36
			for(var i = 0; i < invalid.length; i++){
37
				if(elemname == invalid[i]){ return true; }
38
			}
39
			return false;
40
		}
41
	};
42
 
43
	var _undef = function(name,object){
44
                return (typeof object[name] == "undefined");
45
        };
46
 
47
	// Filters are applied before fields are validated.
48
	// Trim removes white space at the front and end of the fields.
49
	if(profile.trim instanceof Array){
50
		for(var i = 0; i < profile.trim.length; i++){
51
			var elem = form[profile.trim[i]];
52
			if(_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; }
53
			elem.value = elem.value.replace(/(^\s*|\s*$)/g, "");
54
		}
55
	}
56
	// Convert to uppercase
57
	if(profile.uppercase instanceof Array){
58
		for(var i = 0; i < profile.uppercase.length; i++){
59
			var elem = form[profile.uppercase[i]];
60
			if(_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; }
61
			elem.value = elem.value.toUpperCase();
62
		}
63
	}
64
	// Convert to lowercase
65
	if(profile.lowercase instanceof Array){
66
		for (var i = 0; i < profile.lowercase.length; i++){
67
			var elem = form[profile.lowercase[i]];
68
			if(_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; }
69
			elem.value = elem.value.toLowerCase();
70
		}
71
	}
72
	// Uppercase first letter
73
	if(profile.ucfirst instanceof Array){
74
		for(var i = 0; i < profile.ucfirst.length; i++){
75
			var elem = form[profile.ucfirst[i]];
76
			if(_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; }
77
			elem.value = elem.value.replace(/\b\w+\b/g, function(word) { return word.substring(0,1).toUpperCase() + word.substring(1).toLowerCase(); });
78
		}
79
	}
80
	// Remove non digits characters from the input.
81
	if(profile.digit instanceof Array){
82
		for(var i = 0; i < profile.digit.length; i++){
83
			var elem = form[profile.digit[i]];
84
			if(_undef("type", elem) || elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; }
85
			elem.value = elem.value.replace(/\D/g, "");
86
		}
87
	}
88
 
89
	// See if required input fields have values missing.
90
	if(profile.required instanceof Array){
91
		for(var i = 0; i < profile.required.length; i++){
92
			if(!dojo.isString(profile.required[i])){ continue; }
93
			var elem = form[profile.required[i]];
94
			// Are textbox, textarea, or password fields blank.
95
			if(!_undef("type", elem)
96
				&& (elem.type == "text" || elem.type == "textarea" || elem.type == "password" || elem.type == "file")
97
				&& /^\s*$/.test(elem.value)){
98
				missing[missing.length] = elem.name;
99
			}
100
			// Does drop-down box have option selected.
101
			else if(!_undef("type", elem) && (elem.type == "select-one" || elem.type == "select-multiple")
102
						&& (elem.selectedIndex == -1
103
						|| /^\s*$/.test(elem.options[elem.selectedIndex].value))){
104
				missing[missing.length] = elem.name;
105
			}
106
			// Does radio button group (or check box group) have option checked.
107
			else if(elem instanceof Array){
108
				var checked = false;
109
				for(var j = 0; j < elem.length; j++){
110
					if (elem[j].checked) { checked = true; }
111
				}
112
				if(!checked){
113
					missing[missing.length] = elem[0].name;
114
				}
115
			}
116
		}
117
	}
118
 
119
	// See if checkbox groups and select boxes have x number of required values.
120
	if(profile.required instanceof Array){
121
		for (var i = 0; i < profile.required.length; i++){
122
			if(!dojo.isObject(profile.required[i])){ continue; }
123
			var elem, numRequired;
124
			for(var name in profile.required[i]){
125
				elem = form[name];
126
				numRequired = profile.required[i][name];
127
			}
128
			// case 1: elem is a check box group
129
			if(elem instanceof Array){
130
				var checked = 0;
131
				for(var j = 0; j < elem.length; j++){
132
					if(elem[j].checked){ checked++; }
133
				}
134
				if(checked < numRequired){
135
					missing[missing.length] = elem[0].name;
136
				}
137
			}
138
			// case 2: elem is a select box
139
			else if(!_undef("type", elem) && elem.type == "select-multiple" ){
140
				var selected = 0;
141
				for(var j = 0; j < elem.options.length; j++){
142
					if (elem.options[j].selected && !/^\s*$/.test(elem.options[j].value)) { selected++; }
143
				}
144
				if(selected < numRequired){
145
					missing[missing.length] = elem.name;
146
				}
147
			}
148
		}
149
	}
150
 
151
	// Dependent fields are required when the target field is present (not blank).
152
	// Todo: Support dependent and target fields that are radio button groups, or select drop-down lists.
153
	// Todo: Make the dependency based on a specific value of the target field.
154
	// Todo: allow dependent fields to have several required values, like {checkboxgroup: 3}.
155
	if(dojo.isObject(profile.dependencies)){
156
		// properties of dependencies object are the names of dependent fields to be checked
157
		for(name in profile.dependencies){
158
			var elem = form[name];	// the dependent element
159
			if(_undef("type", elem)){continue;}
160
			if(elem.type != "text" && elem.type != "textarea" && elem.type != "password"){ continue; } // limited support
161
			if(/\S+/.test(elem.value)){ continue; }	// has a value already
162
			if(results.isMissing(elem.name)){ continue; }	// already listed as missing
163
			var target = form[profile.dependencies[name]];
164
			if(target.type != "text" && target.type != "textarea" && target.type != "password"){ continue; }	// limited support
165
			if(/^\s*$/.test(target.value)){ continue; }	// skip if blank
166
			missing[missing.length] = elem.name;	// ok the dependent field is missing
167
		}
168
	}
169
 
170
	// Find invalid input fields.
171
	if(dojo.isObject(profile.constraints)){
172
		// constraint properties are the names of fields to bevalidated
173
		for(name in profile.constraints){
174
			var elem = form[name];
175
			if(!elem) {continue;}
176
 
177
			// skip if blank - its optional unless required, in which case it
178
			// is already listed as missing.
179
			if(!_undef("tagName",elem)
180
				&& (elem.tagName.toLowerCase().indexOf("input") >= 0
181
					|| elem.tagName.toLowerCase().indexOf("textarea") >= 0)
182
				&& /^\s*$/.test(elem.value)){
183
				continue;
184
			}
185
 
186
			var isValid = true;
187
			// case 1: constraint value is validation function
188
			if(dojo.isFunction(profile.constraints[name])){
189
				isValid = profile.constraints[name](elem.value);
190
			}else if(dojo.isArray(profile.constraints[name])){
191
 
192
				// handle nested arrays for multiple constraints
193
				if(dojo.isArray(profile.constraints[name][0])){
194
					for (var i=0; i<profile.constraints[name].length; i++){
195
						isValid = dojox.validate.evaluateConstraint(profile, profile.constraints[name][i], name, elem);
196
						if(!isValid){ break; }
197
					}
198
				}else{
199
					// case 2: constraint value is array, first elem is function,
200
					// tail is parameters
201
					isValid = dojox.validate.evaluateConstraint(profile, profile.constraints[name], name, elem);
202
				}
203
			}
204
 
205
			if(!isValid){
206
				invalid[invalid.length] = elem.name;
207
			}
208
		}
209
	}
210
 
211
	// Find unequal confirm fields and report them as Invalid.
212
	if(dojo.isObject(profile.confirm)){
213
		for(name in profile.confirm){
214
			var elem = form[name];	// the confirm element
215
			var target = form[profile.confirm[name]];
216
			if (_undef("type", elem) || _undef("type", target) || (elem.type != "text" && elem.type != "textarea" && elem.type != "password")
217
				||(target.type != elem.type)
218
				||(target.value == elem.value)	// it's valid
219
				||(results.isInvalid(elem.name))// already listed as invalid
220
				||(/^\s*$/.test(target.value)))	// skip if blank - only confirm if target has a value
221
			{
222
				continue;
223
			}
224
			invalid[invalid.length] = elem.name;
225
		}
226
	}
227
	return results; // Object
228
};
229
 
230
//TODO: evaluateConstraint doesn't use profile or fieldName args?
231
dojox.validate.evaluateConstraint=function(profile, /*Array*/constraint, fieldName, elem){
232
	// summary:
233
	//	Evaluates dojo.validate.check() constraints that are specified as array
234
	//	arguments
235
	//
236
	// description: The arrays are expected to be in the format of:
237
	//      constraints:{
238
	//              fieldName: [functionToCall, param1, param2, etc.],
239
	//              fieldName: [[functionToCallFirst, param1],[functionToCallSecond,param2]]
240
	//      }
241
	//
242
	//  This function evaluates a single array function in the format of:
243
	//      [functionName, argument1, argument2, etc]
244
	//
245
	//  The function will be parsed out and evaluated against the incoming parameters.
246
	//
247
	// profile: The dojo.validate.check() profile that this evaluation is against.
248
	// constraint: The single [] array of function and arguments for the function.
249
	// fieldName: The form dom name of the field being validated.
250
	// elem: The form element field.
251
 
252
 	var isValidSomething = constraint[0];
253
	var params = constraint.slice(1);
254
	params.unshift(elem.value);
255
	if(typeof isValidSomething != "undefined"){
256
		return isValidSomething.apply(null, params);
257
	}
258
	return false; // Boolean
259
}
260
 
261
}