1/* ======================================================================== 2 * Bootstrap (plugin): validator.js v0.9.0 3 * ======================================================================== 4 * The MIT License (MIT) 5 * 6 * Copyright (c) 2015 Cina Saffary. 7 * Made by @1000hz in the style of Bootstrap 3 era @fat 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a copy 10 * of this software and associated documentation files (the "Software"), to deal 11 * in the Software without restriction, including without limitation the rights 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 * copies of the Software, and to permit persons to whom the Software is 14 * furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included in 17 * all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 * THE SOFTWARE. 26 * ======================================================================== */ 27 28 29+function ($) { 30 'use strict'; 31 32 // VALIDATOR CLASS DEFINITION 33 // ========================== 34 35 var Validator = function (element, options) { 36 this.$element = $(element) 37 this.options = options 38 39 options.errors = $.extend({}, Validator.DEFAULTS.errors, options.errors) 40 41 for (var custom in options.custom) { 42 if (!options.errors[custom]) throw new Error('Missing default error message for custom validator: ' + custom) 43 } 44 45 $.extend(Validator.VALIDATORS, options.custom) 46 47 this.$element.attr('novalidate', true) // disable automatic native validation 48 this.toggleSubmit() 49 50 this.$element.on('input.bs.validator change.bs.validator focusout.bs.validator', $.proxy(this.validateInput, this)) 51 this.$element.on('submit.bs.validator', $.proxy(this.onSubmit, this)) 52 53 this.$element.find('[data-match]').each(function () { 54 var $this = $(this) 55 var target = $this.data('match') 56 57 $(target).on('input.bs.validator', function (e) { 58 $this.val() && $this.trigger('input.bs.validator') 59 }) 60 }) 61 } 62 63 Validator.INPUT_SELECTOR = ':input:not([type="submit"], button):enabled:visible' 64 65 Validator.DEFAULTS = { 66 delay: 500, 67 html: false, 68 disable: true, 69 custom: {}, 70 errors: { 71 match: 'Does not match', 72 minlength: 'Not long enough' 73 }, 74 feedback: { 75 success: 'glyphicon-ok', 76 error: 'glyphicon-remove' 77 } 78 } 79 80 Validator.VALIDATORS = { 81 'native': function ($el) { 82 var el = $el[0] 83 return el.checkValidity ? el.checkValidity() : true 84 }, 85 'match': function ($el) { 86 var target = $el.data('match') 87 return !$el.val() || $el.val() === $(target).val() 88 }, 89 'minlength': function ($el) { 90 var minlength = $el.data('minlength') 91 return !$el.val() || $el.val().length >= minlength 92 } 93 } 94 95 Validator.prototype.validateInput = function (e) { 96 var $el = $(e.target) 97 var prevErrors = $el.data('bs.validator.errors') 98 var errors 99 100 if ($el.is('[type="radio"]')) $el = this.$element.find('input[name="' + $el.attr('name') + '"]') 101 102 this.$element.trigger(e = $.Event('validate.bs.validator', {relatedTarget: $el[0]})) 103 104 if (e.isDefaultPrevented()) return 105 106 var self = this 107 108 this.runValidators($el).done(function (errors) { 109 $el.data('bs.validator.errors', errors) 110 111 errors.length ? self.showErrors($el) : self.clearErrors($el) 112 113 if (!prevErrors || errors.toString() !== prevErrors.toString()) { 114 e = errors.length 115 ? $.Event('invalid.bs.validator', {relatedTarget: $el[0], detail: errors}) 116 : $.Event('valid.bs.validator', {relatedTarget: $el[0], detail: prevErrors}) 117 118 self.$element.trigger(e) 119 } 120 121 self.toggleSubmit() 122 123 self.$element.trigger($.Event('validated.bs.validator', {relatedTarget: $el[0]})) 124 }) 125 } 126 127 128 Validator.prototype.runValidators = function ($el) { 129 var errors = [] 130 var deferred = $.Deferred() 131 var options = this.options 132 133 $el.data('bs.validator.deferred') && $el.data('bs.validator.deferred').reject() 134 $el.data('bs.validator.deferred', deferred) 135 136 function getErrorMessage(key) { 137 return $el.data(key + '-error') 138 || $el.data('error') 139 || key == 'native' && $el[0].validationMessage 140 || options.errors[key] 141 } 142 143 $.each(Validator.VALIDATORS, $.proxy(function (key, validator) { 144 if (($el.data(key) || key == 'native') && !validator.call(this, $el)) { 145 var error = getErrorMessage(key) 146 !~errors.indexOf(error) && errors.push(error) 147 } 148 }, this)) 149 150 if (!errors.length && $el.val() && $el.data('remote')) { 151 this.defer($el, function () { 152 var data = {} 153 data[$el.attr('name')] = $el.val() 154 $.get($el.data('remote'), data) 155 .fail(function (jqXHR, textStatus, error) { errors.push(getErrorMessage('remote') || error) }) 156 .always(function () { deferred.resolve(errors)}) 157 }) 158 } else deferred.resolve(errors) 159 160 return deferred.promise() 161 } 162 163 Validator.prototype.validate = function () {
164 var delay = this.options.delay 165 166 this.options.delay = 0 167 this.$element.find(Validator.INPUT_SELECTOR).trigger('input.bs.validator') 168 this.options.delay = delay 169 170 return this 171 } 172 173 Validator.prototype.showErrors = function ($el) { 174 var method = this.options.html ? 'html' : 'text' 175 176 this.defer($el, function () { 177 var $group = $el.closest('.form-group') 178 var $block = $group.find('.help-block.with-errors') 179 var $feedback = $group.find('.form-control-feedback') 180 var errors = $el.data('bs.validator.errors') 181 182 if (!errors.length) return 183 184 errors = $('<ul/>') 185 .addClass('list-unstyled') 186 .append($.map(errors, function (error) { return $('<li/>')[method](error) })) 187 188 $block.data('bs.validator.originalContent') === undefined && $block.data('bs.validator.originalContent', $block.html()) 189 $block.empty().append(errors) 190 $group.addClass('has-error') 191 192 $feedback.length 193 && $feedback.removeClass(this.options.feedback.success) 194 && $feedback.addClass(this.options.feedback.error) 195 && $group.removeClass('has-success') 196 }) 197 } 198 199 Validator.prototype.clearErrors = function ($el) { 200 var $group = $el.closest('.form-group') 201 var $block = $group.find('.help-block.with-errors') 202 var $feedback = $group.find('.form-control-feedback') 203 204 $block.html($block.data('bs.validator.originalContent')) 205 $group.removeClass('has-error') 206 207 $feedback.length 208 && $feedback.removeClass(this.options.feedback.error) 209 && $feedback.addClass(this.options.feedback.success) 210 && $group.addClass('has-success') 211 } 212 213 Validator.prototype.hasErrors = function () { 214 function fieldErrors() { 215 return !!($(this).data('bs.validator.errors') || []).length 216 } 217 218 return !!this.$element.find(Validator.INPUT_SELECTOR).filter(fieldErrors).length 219 } 220 221 Validator.prototype.isIncomplete = function () { 222 function fieldIncomplete() { 223 return this.type === 'checkbox' ? !this.checked : 224 this.type === 'radio' ? !$('[name="' + this.name + '"]:checked').length : 225 $.trim(this.value) === '' 226 } 227 228 return !!this.$element.find(Validator.INPUT_SELECTOR).filter('[required]').filter(fieldIncomplete).length 229 } 230 231 Validator.prototype.onSubmit = function (e) { 232 this.validate() 233 if (this.isIncomplete() || this.hasErrors()) e.preventDefault() 234 } 235 236 Validator.prototype.toggleSubmit = function () { 237 if(!this.options.disable) return 238 239 var $btn = $('button[type="submit"], input[type="submit"]') 240 .filter('[form="' + this.$element.attr('id') + '"]') 241 .add(this.$element.find('input[type="submit"], button[type="submit"]')) 242 243 $btn.toggleClass('disabled', this.isIncomplete() || this.hasErrors()) 244 } 245 246 Validator.prototype.defer = function ($el, callback) { 247 callback = $.proxy(callback, this) 248 if (!this.options.delay) return callback() 249 window.clearTimeout($el.data('bs.validator.timeout')) 250 $el.data('bs.validator.timeout', window.setTimeout(callback, this.options.delay)) 251 } 252 253 Validator.prototype.destroy = function () { 254 this.$element 255 .removeAttr('novalidate') 256 .removeData('bs.validator') 257 .off('.bs.validator') 258 259 this.$element.find(Validator.INPUT_SELECTOR) 260 .off('.bs.validator') 261 .removeData(['bs.validator.errors', 'bs.validator.deferred']) 262 .each(function () { 263 var $this = $(this) 264 var timeout = $this.data('bs.validator.timeout') 265 window.clearTimeout(timeout) && $this.removeData('bs.validator.timeout') 266 }) 267 268 this.$element.find('.help-block.with-errors').each(function () { 269 var $this = $(this) 270 var originalContent = $this.data('bs.validator.originalContent') 271 272 $this 273 .removeData('bs.validator.originalContent') 274 .html(originalContent) 275 }) 276 277 this.$element.find('input[type="submit"], button[type="submit"]').removeClass('disabled') 278 279 this.$element.find('.has-error').removeClass('has-error') 280 281 return this 282 } 283 284 // VALIDATOR PLUGIN DEFINITION 285 // =========================== 286 287 288 function Plugin(option) { 289 return this.each(function () { 290 var $this = $(this) 291 var options = $.extend({}, Validator.DEFAULTS, $this.data(), typeof option == 'object' && option) 292 var data = $this.data('bs.validator') 293 294 if (!data && option == 'destroy') return 295 if (!data) $this.data('bs.validator', (data = new Validator(this, options))) 296 if (typeof option == 'string') data[option]() 297 }) 298 } 299 300 var old = $.fn.validator 301 302 $.fn.validator = Plugin 303 $.fn.validator.Constructor = Validator 304 305 306 // VALIDATOR NO CONFLICT 307 // ===================== 308 309 $.fn.validator.noConflict = function () { 310 $.fn.validator = old 311 return this 312 } 313 314 315 // VALIDATOR DATA-API 316 // ================== 317 318 $(window).on('load', function () { 319 $('form[data-toggle="validator"]').each(function () { 320 var $form = $(this) 321 Plugin.call($form, $form.data()) 322 }) 323 }) 324 325}(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.