123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- /**
- * Yii validation module.
- *
- * This JavaScript module provides the validation methods for the built-in validators.
- *
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- * @author Qiang Xue <[email protected]>
- * @since 2.0
- */
- yii.validation = (function ($) {
- var isEmpty = function (value, trim) {
- return value === null || value === undefined || value == []
- || value === '' || trim && $.trim(value) === '';
- };
- var addMessage = function (messages, message, value) {
- messages.push(message.replace(/\{value\}/g, value));
- };
- return {
- required: function (value, messages, options) {
- var valid = false;
- 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;
- }
- if (!valid) {
- addMessage(messages, options.message, value);
- }
- },
- boolean: function (value, messages, options) {
- if (options.skipOnEmpty && isEmpty(value)) {
- return;
- }
- var valid = !options.strict && (value == options.trueValue || value == options.falseValue)
- || options.strict && (value === options.trueValue || value === options.falseValue);
- if (!valid) {
- addMessage(messages, options.message, value);
- }
- },
- string: function (value, messages, options) {
- if (options.skipOnEmpty && isEmpty(value)) {
- return;
- }
- if (typeof value !== 'string') {
- addMessage(messages, options.message, value);
- return;
- }
- if (options.min !== undefined && value.length < options.min) {
- addMessage(messages, options.tooShort, value);
- }
- if (options.max !== undefined && value.length > options.max) {
- addMessage(messages, options.tooLong, value);
- }
- if (options.is !== undefined && value.length != options.is) {
- addMessage(messages, options.is, value);
- }
- },
- number: function (value, messages, options) {
- if (options.skipOnEmpty && isEmpty(value)) {
- return;
- }
- if (typeof value === 'string' && !value.match(options.pattern)) {
- addMessage(messages, options.message, value);
- return;
- }
- if (options.min !== undefined && value < options.min) {
- addMessage(messages, options.tooSmall, value);
- }
- if (options.max !== undefined && value > options.max) {
- addMessage(messages, options.tooBig, value);
- }
- },
- range: function (value, messages, options) {
- if (options.skipOnEmpty && isEmpty(value)) {
- return;
- }
- var valid = !options.not && $.inArray(value, options.range) > -1
- || options.not && $.inArray(value, options.range) == -1;
- if (!valid) {
- addMessage(messages, options.message, value);
- }
- },
- regularExpression: function (value, messages, options) {
- if (options.skipOnEmpty && isEmpty(value)) {
- return;
- }
- if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) {
- addMessage(messages, options.message, value);
- }
- },
- email: function (value, messages, options) {
- if (options.skipOnEmpty && isEmpty(value)) {
- return;
- }
- var valid = true;
- if (options.enableIDN) {
- var regexp = /^(.*<?)(.*)@(.*)(>?)$/,
- matches = regexp.exec(value);
- if (matches === null) {
- valid = false;
- } else {
- value = matches[1] + punycode.toASCII(matches[2]) + '@' + punycode.toASCII(matches[3]) + matches[4];
- }
- }
- if (!valid || !(value.match(options.pattern) || (options.allowName && value.match(options.fullPattern)))) {
- addMessage(messages, options.message, value);
- }
- },
- url: function (value, messages, options) {
- if (options.skipOnEmpty && isEmpty(value)) {
- return;
- }
- if (options.defaultScheme && !value.match(/:\/\//)) {
- value = options.defaultScheme + '://' + value;
- }
- var valid = true;
- if (options.enableIDN) {
- var regexp = /^([^:]+):\/\/([^\/]+)(.*)$/,
- matches = regexp.exec(value);
- if (matches === null) {
- valid = false;
- } else {
- value = matches[1] + '://' + punycode.toASCII(matches[2]) + matches[3];
- }
- }
- if (!valid || !value.match(options.pattern)) {
- addMessage(messages, options.message, value);
- }
- },
- captcha: function (value, messages, options) {
- if (options.skipOnEmpty && isEmpty(value)) {
- return;
- }
- // 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];
- }
- var v = options.caseSensitive ? value : value.toLowerCase();
- for (var i = v.length - 1, h = 0; i >= 0; --i) {
- h += v.charCodeAt(i);
- }
- if (h != hash) {
- addMessage(messages, options.message, value);
- }
- },
- compare: function (value, messages, options) {
- if (options.skipOnEmpty && isEmpty(value)) {
- return;
- }
- var compareValue, valid = true;
- if (options.compareAttribute === undefined) {
- compareValue = options.compareValue;
- } else {
- compareValue = $('#' + options.compareAttribute).val();
- }
- 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;
- default:
- valid = false;
- break;
- }
- if (!valid) {
- addMessage(messages, options.message, value);
- }
- }
- };
- })(jQuery);
|