You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
221 lines
5.4 KiB
221 lines
5.4 KiB
12 years ago
|
/**
|
||
12 years ago
|
* Yii validation module.
|
||
12 years ago
|
*
|
||
12 years ago
|
* This JavaScript module provides the validation methods for the built-in validators.
|
||
12 years ago
|
*
|
||
|
* @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
|
||
|
*/
|
||
|
|
||
12 years ago
|
yii.validation = (function ($) {
|
||
12 years ago
|
var isEmpty = function (value, trim) {
|
||
|
return value === null || value === undefined || value == []
|
||
|
|| value === '' || trim && $.trim(value) === '';
|
||
|
};
|
||
|
|
||
|
return {
|
||
|
required: function (value, messages, options) {
|
||
12 years ago
|
var valid = false;
|
||
12 years ago
|
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;
|
||
|
}
|
||
|
|
||
12 years ago
|
if (!valid) {
|
||
12 years ago
|
messages.push(options.message);
|
||
|
}
|
||
12 years ago
|
},
|
||
|
|
||
12 years ago
|
boolean: function (value, messages, options) {
|
||
12 years ago
|
if (options.skipOnEmpty && isEmpty(value)) {
|
||
|
return;
|
||
|
}
|
||
12 years ago
|
var valid = !options.strict && (value == options.trueValue || value == options.falseValue)
|
||
|
|| options.strict && (value === options.trueValue || value === options.falseValue);
|
||
12 years ago
|
|
||
12 years ago
|
if (!valid) {
|
||
12 years ago
|
messages.push(options.message);
|
||
|
}
|
||
12 years ago
|
},
|
||
|
|
||
|
string: function (value, messages, options) {
|
||
|
if (options.skipOnEmpty && isEmpty(value)) {
|
||
|
return;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
if (typeof value !== 'string') {
|
||
|
messages.push(options.message);
|
||
|
return;
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
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);
|
||
|
}
|
||
12 years ago
|
},
|
||
|
|
||
12 years ago
|
number: function (value, messages, options) {
|
||
12 years ago
|
if (options.skipOnEmpty && isEmpty(value)) {
|
||
|
return;
|
||
|
}
|
||
12 years ago
|
|
||
|
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);
|
||
12 years ago
|
|
||
12 years ago
|
if (!valid) {
|
||
12 years ago
|
messages.push(options.message);
|
||
|
}
|
||
12 years ago
|
},
|
||
|
|
||
12 years ago
|
regularExpression: function (value, messages, options) {
|
||
|
if (options.skipOnEmpty && isEmpty(value)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) {
|
||
12 years ago
|
messages.push(options.message)
|
||
|
}
|
||
|
},
|
||
|
|
||
12 years ago
|
email: function (value, messages, options) {
|
||
|
if (options.skipOnEmpty && isEmpty(value)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
var valid = true;
|
||
|
|
||
12 years ago
|
if (options.enableIDN) {
|
||
|
var regexp = /^(.*)@(.*)$/,
|
||
|
matches = regexp.exec(value);
|
||
12 years ago
|
if (matches === null) {
|
||
|
valid = false;
|
||
|
} else {
|
||
|
value = punycode.toASCII(matches[1]) + '@' + punycode.toASCII(matches[2]);
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
if (!valid || !(value.match(options.pattern) && (!options.allowName || value.match(options.fullPattern)))) {
|
||
12 years ago
|
messages.push(options.message);
|
||
|
}
|
||
12 years ago
|
},
|
||
|
|
||
12 years ago
|
url: function (value, messages, options) {
|
||
12 years ago
|
if (options.skipOnEmpty && isEmpty(value)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
if (options.defaultScheme && !value.match(/:\/\//)) {
|
||
|
value = options.defaultScheme + '://' + value;
|
||
|
}
|
||
|
|
||
12 years ago
|
var valid = true;
|
||
|
|
||
12 years ago
|
if (options.enableIDN) {
|
||
12 years ago
|
var regexp = /^([^:]+):\/\/([^\/]+)(.*)$/,
|
||
12 years ago
|
matches = regexp.exec(value);
|
||
12 years ago
|
if (matches === null) {
|
||
|
valid = false;
|
||
|
} else {
|
||
|
value = matches[1] + '://' + punycode.toASCII(matches[2]) + matches[3];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!valid || !value.match(options.pattern)) {
|
||
12 years ago
|
messages.push(options.message);
|
||
12 years ago
|
}
|
||
12 years ago
|
},
|
||
12 years ago
|
|
||
12 years ago
|
captcha: function (value, messages, options) {
|
||
12 years ago
|
if (options.skipOnEmpty && isEmpty(value)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
// 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];
|
||
12 years ago
|
}
|
||
12 years ago
|
var v = options.caseSensitive ? value : value.toLowerCase();
|
||
|
for (var i = v.length - 1, h = 0; i >= 0; --i) {
|
||
|
h += v.charCodeAt(i);
|
||
12 years ago
|
}
|
||
12 years ago
|
if (h != hash) {
|
||
12 years ago
|
messages.push(options.message);
|
||
12 years ago
|
}
|
||
12 years ago
|
},
|
||
|
|
||
12 years ago
|
compare: function (value, messages, options) {
|
||
12 years ago
|
if (options.skipOnEmpty && isEmpty(value)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
var compareValue, valid = true;
|
||
|
if (options.compareAttribute === undefined) {
|
||
|
compareValue = options.compareValue;
|
||
|
} else {
|
||
|
compareValue = $('#' + options.compareAttribute).val();
|
||
12 years ago
|
}
|
||
12 years ago
|
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;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
if (!valid) {
|
||
12 years ago
|
messages.push(options.message);
|
||
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
};
|
||
|
})(jQuery);
|