yii.validation.js 5.8 KB
Newer Older
Qiang Xue committed
1
/**
Qiang Xue committed
2
 * Yii validation module.
Qiang Xue committed
3
 *
Qiang Xue committed
4
 * This JavaScript module provides the validation methods for the built-in validators.
Qiang Xue committed
5 6 7 8 9 10 11 12
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */

Qiang Xue committed
13
yii.validation = (function ($) {
Qiang Xue committed
14 15 16 17 18
	var isEmpty = function (value, trim) {
		return value === null || value === undefined || value == []
			|| value === '' || trim && $.trim(value) === '';
	};

19 20 21 22
	var addMessage = function (messages, message, value) {
		messages.push(message.replace(/\{value\}/g, value));
	};

Qiang Xue committed
23 24
	return {
		required: function (value, messages, options) {
Qiang Xue committed
25
			var valid = false;
Qiang Xue committed
26 27 28 29 30 31 32 33
			if (options.requiredValue === undefined) {
				if (options.strict && value !== undefined || !options.strict && !isEmpty(value, true)) {
					valid = true;
				}
			} else if (!options.strict && value == options.requiredValue || options.strict && value === options.requiredValue) {
				valid = true;
			}

Qiang Xue committed
34
			if (!valid) {
35
				addMessage(messages, options.message, value);
36
			}
Qiang Xue committed
37 38
		},

Qiang Xue committed
39
		boolean: function (value, messages, options) {
40 41 42
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}
Qiang Xue committed
43 44
			var valid = !options.strict && (value == options.trueValue || value == options.falseValue)
				|| options.strict && (value === options.trueValue || value === options.falseValue);
45

Qiang Xue committed
46
			if (!valid) {
47
				addMessage(messages, options.message, value);
48
			}
Qiang Xue committed
49 50 51 52 53
		},

		string: function (value, messages, options) {
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
54
			}
Qiang Xue committed
55 56

			if (typeof value !== 'string') {
57
				addMessage(messages, options.message, value);
Qiang Xue committed
58
				return;
59 60
			}

Qiang Xue committed
61
			if (options.min !== undefined && value.length < options.min) {
62
				addMessage(messages, options.tooShort, value);
Qiang Xue committed
63 64
			}
			if (options.max !== undefined && value.length > options.max) {
65
				addMessage(messages, options.tooLong, value);
Qiang Xue committed
66 67
			}
			if (options.is !== undefined && value.length != options.is) {
68
				addMessage(messages, options.is, value);
Qiang Xue committed
69
			}
70 71
		},

Qiang Xue committed
72
		number: function (value, messages, options) {
Qiang Xue committed
73 74 75
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}
Qiang Xue committed
76 77

			if (typeof value === 'string' && !value.match(options.pattern)) {
78
				addMessage(messages, options.message, value);
Qiang Xue committed
79 80 81 82
				return;
			}

			if (options.min !== undefined && value < options.min) {
83
				addMessage(messages, options.tooSmall, value);
Qiang Xue committed
84 85
			}
			if (options.max !== undefined && value > options.max) {
86
				addMessage(messages, options.tooBig, value);
Qiang Xue committed
87 88 89 90 91 92 93
			}
		},

		range: function (value, messages, options) {
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}
94 95
			var valid = !options.not && $.inArray(value, options.range) > -1
				|| options.not && $.inArray(value, options.range) == -1;
Qiang Xue committed
96

Qiang Xue committed
97
			if (!valid) {
98
				addMessage(messages, options.message, value);
99
			}
100 101
		},

102 103 104 105 106
		regularExpression: function (value, messages, options) {
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}

107
			if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) {
108
				addMessage(messages, options.message, value);
109 110 111
			}
		},

112 113 114 115 116
		email: function (value, messages, options) {
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}

117 118
			var valid = true;

119
			if (options.enableIDN) {
120
				var regexp = /^(.*<?)(.*)@(.*)(>?)$/,
121
					matches = regexp.exec(value);
122 123 124
				if (matches === null) {
					valid = false;
				} else {
125
					value = matches[1] + punycode.toASCII(matches[2]) + '@' + punycode.toASCII(matches[3]) + matches[4];
126 127
				}
			}
128

129
			if (!valid || !(value.match(options.pattern) || (options.allowName && value.match(options.fullPattern)))) {
130
				addMessage(messages, options.message, value);
131
			}
132 133
		},

134
		url: function (value, messages, options) {
135 136 137 138
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}

139 140 141 142
			if (options.defaultScheme && !value.match(/:\/\//)) {
				value = options.defaultScheme + '://' + value;
			}

143 144
			var valid = true;

145
			if (options.enableIDN) {
146
				var regexp = /^([^:]+):\/\/([^\/]+)(.*)$/,
147
					matches = regexp.exec(value);
148 149 150 151 152 153 154 155
				if (matches === null) {
					valid = false;
				} else {
					value = matches[1] + '://' + punycode.toASCII(matches[2]) + matches[3];
				}
			}

			if (!valid || !value.match(options.pattern)) {
156
				addMessage(messages, options.message, value);
157
			}
158
		},
159

Qiang Xue committed
160
		captcha: function (value, messages, options) {
161 162 163 164
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}

Qiang Xue committed
165 166 167 168 169 170
			// CAPTCHA may be updated via AJAX and the updated hash is stored in body data
			var hash = $('body').data(options.hashKey);
			if (hash == null) {
				hash = options.hash;
			} else {
				hash = hash[options.caseSensitive ? 0 : 1];
171
			}
Qiang Xue committed
172 173 174
			var v = options.caseSensitive ? value : value.toLowerCase();
			for (var i = v.length - 1, h = 0; i >= 0; --i) {
				h += v.charCodeAt(i);
175
			}
Qiang Xue committed
176
			if (h != hash) {
177
				addMessage(messages, options.message, value);
178
			}
179 180
		},

Qiang Xue committed
181
		compare: function (value, messages, options) {
182 183 184 185
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}

Qiang Xue committed
186 187 188 189 190
			var compareValue, valid = true;
			if (options.compareAttribute === undefined) {
				compareValue = options.compareValue;
			} else {
				compareValue = $('#' + options.compareAttribute).val();
191
			}
Qiang Xue committed
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
			switch (options.operator) {
				case '==':
					valid = value == compareValue;
					break;
				case '===':
					valid = value === compareValue;
					break;
				case '!=':
					valid = value != compareValue;
					break;
				case '!==':
					valid = value !== compareValue;
					break;
				case '>':
					valid = value > compareValue;
					break;
				case '>=':
					valid = value >= compareValue;
					break;
				case '<':
					valid = value < compareValue;
					break;
				case '<=':
					valid = value <= compareValue;
					break;
217 218 219
				default:
					valid = false;
					break;
220
			}
Qiang Xue committed
221

Qiang Xue committed
222
			if (!valid) {
223
				addMessage(messages, options.message, value);
224
			}
225
		}
Qiang Xue committed
226 227
	};
})(jQuery);