summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts/base/validators.js
blob: 427b46fcf41757af0c1865a9078138d52d094463 (plain)
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

Prado.Validation.TRequiredFieldValidator=function(){
    var inputType = this.control.getAttribute("type");
    if(inputType == 'file'){
        return true;
    }
    else{
        var trim=Prado.Util.trim;
        var a=trim(Form.Element.getValue(this.control));
        var b=trim(this.attr.initialvalue);
        return(a!=b);
    }
}


Prado.Validation.TRegularExpressionValidator = function()
{
	var trim = Prado.Util.trim;
	var value = trim(Form.Element.getValue(this.control));
    if (value == "") return true;
    var rx = new RegExp(this.attr.validationexpression);
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}

Prado.Validation.TEmailAddressValidator = Prado.Validation.TRegularExpressionValidator;

Prado.Validation.TCustomValidator = function()
{
	var value = isNull(this.control) ? null : $F(this.control);
    var func = this.attr.clientvalidationfunction;
	eval("var validate = "+func);
    return validate && isFunction(validate) ? validate(this, value) : true;
}

Prado.Validation.TRangeValidator = function()
{
	var trim = Prado.Util.trim;
	var value = trim(Form.Element.getValue(this.control));
    if (value == "") return true;

    var minval = this.attr.minimumvalue;
    var maxval = this.attr.maximumvalue;

	if (undef(minval) && undef(maxval))
        return true;

    if (minval == "") minval = 0;
	if (maxval == "") maxval = 0;
	
	var dataType = this.attr.type;

	if(undef(dataType))
	    return (parseFloat(value) >= parseFloat(minval)) && (parseFloat(value) <= parseFloat(maxval));

	//now do datatype range check.
	var min = this.convert(dataType, minval);
	var max = this.convert(dataType, maxval);
	value = this.convert(dataType, value);	
	return value >= min && value <= max;
}

Prado.Validation.TCompareValidator = function()
{
	var trim = Prado.Util.trim;
    var value = trim(Form.Element.getValue(this.control));
    if (value.length == 0) return true;

    var compareTo;

    var comparee = $(this.attr.controlhookup);;

	if(comparee)
		compareTo = trim(Form.Element.getValue(comparee));
	else
	{
		compareTo = isString(this.attr.valuetocompare) ? this.attr.valuetocompare : "";
	}

	var compare = Prado.Validation.TCompareValidator.compare;

    var isValid =  compare.bind(this)(value, compareTo);

	//update the comparee control css class name and add onchange event once.
	if(comparee)
	{
		var className = this.attr.controlcssclass;
		if(isString(className) && className.length>0)
			Element.condClassName(comparee, className, !isValid);
		if(undef(this.observingComparee))
		{
			Event.observe(comparee, "change", this.validate.bind(this));
			this.observingComparee = true;
		}
	}
	return isValid;
}

/**
 * Compare the two values, also performs data type check.
 * @param {string} value to compare with
 * @param {string} value to compare
 * @type {boolean} true if comparison or type check is valid, false otherwise.
 */
Prado.Validation.TCompareValidator.compare = function(operand1, operand2)
{
	var op1, op2;
	if ((op1 = this.convert(this.attr.type, operand1)) == null)
		return false;
	if (this.attr.operator == "DataTypeCheck")
        return true;
	if ((op2 = this.convert(this.attr.type, operand2)) == null)
        return true;
    switch (this.attr.operator) 
	{
        case "NotEqual":
            return (op1 != op2);
        case "GreaterThan":
            return (op1 > op2);
        case "GreaterThanEqual":
            return (op1 >= op2);
        case "LessThan":
            return (op1 < op2);
        case "LessThanEqual":
            return (op1 <= op2);
        default:
            return (op1 == op2);
    }
}

Prado.Validation.TRequiredListValidator = function()
{
	var min = undef(this.attr.min) ? Number.NEGATIVE_INFINITY : parseInt(this.attr.min);
	var max = undef(this.attr.max) ? Number.POSITIVE_INFINITY : parseInt(this.attr.max);

	var elements = document.getElementsByName(this.attr.selector);

	if(elements.length <= 0)
		elements = document.getElementsBySelector(this.attr.selector);

	if(elements.length <= 0)
		return true;
	
	var required = new Array();
	if(isString(this.attr.required) && this.attr.required.length > 0)
		required = this.attr.required.split(/,\s* /);

	var isValid = true;

	var validator = Prado.Validation.TRequiredListValidator;

	switch(elements[0].type)
	{
		case 'radio':
		case 'checkbox':
			isValid = validator.IsValidRadioList(elements, min, max, required);
			break;
		case 'select-multiple':
			isValid = validator.IsValidSelectMultipleList(elements, min, max, required);
			break;
	}

	var className = this.attr.elementcssclass;
	if(isString(className) && className.length>0)
		map(elements, function(element){ condClass(element, className, !isValid); });
	if(undef(this.observingRequiredList))
	{
		Event.observe(elements, "change", this.validate.bind(this));
		this.observingRequiredList = true;
	}
	return isValid;
}

//radio group selection
Prado.Validation.TRequiredListValidator.IsValidRadioList = function(elements, min, max, required)
{
	var checked = 0;
	var values = new Array();
	for(var i = 0; i < elements.length; i++)
	{
		if(elements[i].checked)
		{
			checked++;
			values.push(elements[i].value);
		}
	}
	return Prado.Validation.TRequiredListValidator.IsValidList(checked, values, min, max, required);
}

//multiple selection check
Prado.Validation.TRequiredListValidator.IsValidSelectMultipleList = function(elements, min, max, required)
{
	var checked = 0;
	var values = new Array();
	for(var i = 0; i < elements.length; i++)
	{
		var selection = elements[i];
		for(var j = 0; j < selection.options.length; j++)
		{
			if(selection.options[j].selected)
			{
				checked++;
				values.push(selection.options[j].value);
			}
		}
	}
	return Prado.Validation.TRequiredListValidator.IsValidList(checked, values, min, max, required);
}

//check if the list was valid
Prado.Validation.TRequiredListValidator.IsValidList = function(checkes, values, min, max, required)
{
	var exists = true;

	if(required.length > 0)
	{
		//required and the values must at least be have same lengths
		if(values.length < required.length)
			return false;
		for(var k = 0; k < required.length; k++)
			exists = exists && values.contains(required[k]);
	}
	
	return exists && checkes >= min && checkes <= max;
}