yii.validation.js 10.1 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 19
    var pub = {
        isEmpty: function (value) {
            return value === null || value === undefined || value == [] || value === '';
        },

        addMessage: function (messages, message, value) {
20
            messages.push(message.replace(/\{value\}/g, value));
Qiang Xue committed
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
        },

        required: function (value, messages, options) {
            var valid = false;
            if (options.requiredValue === undefined) {
                var isString = typeof value == 'string' || value instanceof String;
                if (options.strict && value !== undefined || !options.strict && !pub.isEmpty(isString ? $.trim(value) : value)) {
                    valid = true;
                }
            } else if (!options.strict && value == options.requiredValue || options.strict && value === options.requiredValue) {
                valid = true;
            }

            if (!valid) {
                pub.addMessage(messages, options.message, value);
            }
        },

        boolean: function (value, messages, options) {
            if (options.skipOnEmpty && pub.isEmpty(value)) {
                return;
            }
            var valid = !options.strict && (value == options.trueValue || value == options.falseValue)
                || options.strict && (value === options.trueValue || value === options.falseValue);

            if (!valid) {
                pub.addMessage(messages, options.message, value);
            }
        },

        string: function (value, messages, options) {
            if (options.skipOnEmpty && pub.isEmpty(value)) {
                return;
            }

            if (typeof value !== 'string') {
                pub.addMessage(messages, options.message, value);
                return;
            }

            if (options.min !== undefined && value.length < options.min) {
                pub.addMessage(messages, options.tooShort, value);
            }
            if (options.max !== undefined && value.length > options.max) {
                pub.addMessage(messages, options.tooLong, value);
            }
            if (options.is !== undefined && value.length != options.is) {
Sergeygithub committed
68
                pub.addMessage(messages, options.notEqual, value);
Qiang Xue committed
69 70
            }
        },
71 72

        file: function (value, messages, options, attribute) {
73
            var files = $(attribute.input).get(0).files,
74 75 76
                index, ext;

            if (options.message && !files) {
77 78 79
                pub.addMessage(messages, options.message, value);
            }

80
            if (!options.skipOnEmpty && files.length == 0) {
81
                pub.addMessage(messages, options.uploadRequired, value);
82
            } else if (files.length == 0) {
83 84
                return;
            }
85 86

            if (options.maxFiles && options.maxFiles < files.length) {
87 88
                pub.addMessage(messages, options.tooMany);
            }
89 90 91

            $.each(files, function (i, file) {
                if (options.extensions && options.extensions.length > 0) {
92
                    index = file.name.lastIndexOf('.');
93 94

                    if (!~index) {
95 96
                        ext = '';
                    } else {
97
                        ext = file.name.substr(index + 1, file.name.length).toLowerCase();
98
                    }
99 100 101

                    if (!~options.extensions.indexOf(ext)) {
                        messages.push(options.wrongExtension.replace(/\{file\}/g, file.name));
102 103
                    }
                }
104 105 106 107

                if (options.mimeTypes && options.mimeTypes.length > 0) {
                    if (!~options.mimeTypes.indexOf(file.type)) {
                        messages.push(options.wrongMimeType.replace(/\{file\}/g, file.name));
108 109
                    }
                }
110 111 112

                if (options.maxSize && options.maxSize < file.size) {
                    messages.push(options.tooBig.replace(/\{file\}/g, file.name));
113
                }
114 115 116

                if (options.maxSize && options.minSize > file.size) {
                    messages.push(options.tooSmall.replace(/\{file\}/g, file.name));
117
                }
118

119 120
            });
        },
Qiang Xue committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

        number: function (value, messages, options) {
            if (options.skipOnEmpty && pub.isEmpty(value)) {
                return;
            }

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

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

        range: function (value, messages, options) {
            if (options.skipOnEmpty && pub.isEmpty(value)) {
                return;
            }

145 146 147 148 149
            if (!options.allowArray && $.isArray(value)) {
                pub.addMessage(messages, options.message, value);
                return;
            }

150 151
            var inArray = true;

152
            $.each($.isArray(value) ? value : [value], function(i, v) {
153 154 155 156 157 158 159 160
                if ($.inArray(v, options.range) == -1) {
                    inArray = false;
                    return false;
                } else {
                    return true;
                }
            });

161
            if (options.not === inArray) {
Qiang Xue committed
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
                pub.addMessage(messages, options.message, value);
            }
        },

        regularExpression: function (value, messages, options) {
            if (options.skipOnEmpty && pub.isEmpty(value)) {
                return;
            }

            if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) {
                pub.addMessage(messages, options.message, value);
            }
        },

        email: function (value, messages, options) {
            if (options.skipOnEmpty && pub.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)))) {
                pub.addMessage(messages, options.message, value);
            }
        },

        url: function (value, messages, options) {
            if (options.skipOnEmpty && pub.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)) {
                pub.addMessage(messages, options.message, value);
            }
        },

        captcha: function (value, messages, options) {
            if (options.skipOnEmpty && pub.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) {
                pub.addMessage(messages, options.message, value);
            }
        },

        compare: function (value, messages, options) {
            if (options.skipOnEmpty && pub.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 '>':
270
                    valid = parseFloat(value) > parseFloat(compareValue);
Qiang Xue committed
271 272
                    break;
                case '>=':
273
                    valid = parseFloat(value) >= parseFloat(compareValue);
Qiang Xue committed
274 275
                    break;
                case '<':
276
                    valid = parseFloat(value) < parseFloat(compareValue);
Qiang Xue committed
277 278
                    break;
                case '<=':
279
                    valid = parseFloat(value) <= parseFloat(compareValue);
Qiang Xue committed
280 281 282 283 284 285 286 287 288 289 290 291
                    break;
                default:
                    valid = false;
                    break;
            }

            if (!valid) {
                pub.addMessage(messages, options.message, value);
            }
        }
    };
    return pub;
Qiang Xue committed
292
})(jQuery);