yii.validation.js 4.85 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 validaotrs.
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 19 20
	var isEmpty = function (value, trim) {
		return value === null || value === undefined || value == []
			|| value === '' || trim && $.trim(value) === '';
	};

	return {
		required: function (value, messages, options) {
Qiang Xue committed
21
			var valid = false;
Qiang Xue committed
22 23 24 25 26 27 28 29
			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
30
			if (!valid) {
31 32
				messages.push(options.message);
			}
Qiang Xue committed
33 34
		},

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

Qiang Xue committed
42
			if (!valid) {
43 44
				messages.push(options.message);
			}
Qiang Xue committed
45 46 47 48 49
		},

		string: function (value, messages, options) {
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
50
			}
Qiang Xue committed
51 52 53 54

			if (typeof value !== 'string') {
				messages.push(options.message);
				return;
55 56
			}

Qiang Xue committed
57 58 59 60 61 62 63 64 65
			if (options.min !== undefined && value.length < options.min) {
				messages.push(options.tooShort);
			}
			if (options.max !== undefined && value.length > options.max) {
				messages.push(options.tooLong);
			}
			if (options.is !== undefined && value.length != options.is) {
				messages.push(options.is);
			}
66 67
		},

Qiang Xue committed
68
		number: function (value, messages, options) {
Qiang Xue committed
69 70 71
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}
Qiang Xue committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

			if (typeof value === 'string' && !value.match(options.pattern)) {
				messages.push(options.message);
				return;
			}

			if (options.min !== undefined && value < options.min) {
				messages.push(options.tooSmall);
			}
			if (options.max !== undefined && value > options.max) {
				messages.push(options.tooBig);
			}
		},

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

Qiang Xue committed
93
			if (!valid) {
94 95
				messages.push(options.message);
			}
96 97
		},

98 99 100 101 102
		regularExpression: function (value, messages, options) {
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}

103
			if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) {
104 105 106 107
				messages.push(options.message)
			}
		},

108 109 110 111 112 113 114
		email: function (value, messages, options) {
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}

			var valid = value.match(options.pattern) && (!options.allowName || value.match(options.fullPattern));

Qiang Xue committed
115
			if (!valid) {
116 117
				messages.push(options.message);
			}
118 119
		},

120
		url: function (value, messages, options) {
121 122 123 124
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}

125 126 127 128
			if (options.defaultScheme && !value.match(/:\/\//)) {
				value = options.defaultScheme + '://' + value;
			}

129
			if (!value.match(options.pattern)) {
130
				messages.push(options.message);
131
			}
132
		},
133

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

Qiang Xue committed
139 140 141 142 143 144
			// 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];
145
			}
Qiang Xue committed
146 147 148
			var v = options.caseSensitive ? value : value.toLowerCase();
			for (var i = v.length - 1, h = 0; i >= 0; --i) {
				h += v.charCodeAt(i);
149
			}
Qiang Xue committed
150
			if (h != hash) {
Qiang Xue committed
151
				messages.push(options.message);
152
			}
153 154
		},

Qiang Xue committed
155
		compare: function (value, messages, options) {
156 157 158 159
			if (options.skipOnEmpty && isEmpty(value)) {
				return;
			}

Qiang Xue committed
160 161 162 163 164
			var compareValue, valid = true;
			if (options.compareAttribute === undefined) {
				compareValue = options.compareValue;
			} else {
				compareValue = $('#' + options.compareAttribute).val();
165
			}
Qiang Xue committed
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
			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;
191
			}
Qiang Xue committed
192

Qiang Xue committed
193
			if (!valid) {
194 195
				messages.push(options.message);
			}
196
		}
Qiang Xue committed
197 198
	};
})(jQuery);