1/* ======================================================================== 2 * Bootstrap (plugin): validator.js v0.8.1 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 var inputSelector = ':input:not([type="submit"], button):enabled:visible' 33 // VALIDATOR CLASS DEFINITION 34 // ========================== 35 36 var Validator = function (element, options) { 37 this.$element = $(element) 38 this.options = options 39 40 options.errors = $.extend({}, Validator.DEFAULTS.errors, options.errors) 41 42 for (var custom in options.custom) { 43 if (!options.errors[custom]) throw new Error('Missing default error message for custom validator: ' + custom) 44 } 45 46 $.extend(Validator.VALIDATORS, options.custom) 47 48 this.$element.attr('novalidate', true) // disable automatic native validation 49 this.toggleSubmit() 50 51 this.$element.on('input.bs.validator change.bs.validator focusout.bs.validator', $.proxy(this.validateInput, this)) 52 this.$element.on('submit.bs.validator', $.proxy(this.onSubmit, this)) 53 54 this.$element.find('[data-match]').each(function () { 55 var $this = $(this) 56 var target = $this.data('match') 57 58 $(target).on('input.bs.validator', function (e) { 59 $this.val() && $this.trigger('input.bs.validator') 60 }) 61 }) 62 } 63 64 Validator.DEFAULTS = { 65 delay: 500, 66 html: false, 67 disable: true, 68 custom: {}, 69 errors: { 70 match: 'Does not match', 71 minlength: 'Not long enough' 72 }, 73 feedback: { 74 success: 'glyphicon-ok', 75 error: 'glyphicon-warning-sign' 76 } 77 } 78 79 Validator.VALIDATORS = { 80 native: function ($el) { 81 var el = $el[0] 82 return el.checkValidity ? el.checkValidity() : true 83 }, 84 match: function ($el) { 85 var target = $el.data('match') 86 return !$el.val() || $el.val() === $(target).val() 87 }, 88 minlength: function ($el) { 89 var minlength = $el.data('minlength') 90 return !$el.val() || $el.val().length >= minlength 91 } 92 } 93 94 Validator.prototype.validateInput = function (e) { 95 var $el = $(e.target) 96 var prevErrors = $el.data('bs.validator.errors') 97 var errors 98 99 if ($el.is('[type="radio"]')) $el = this.$element.find('input[name="' + $el.attr('name') + '"]') 100 101 this.$element.trigger(e = $.Event('validate.bs.validator', {relatedTarget: $el[0]})) 102 103 if (e.isDefaultPrevented()) return 104 105 var self = this 106 107 this.runValidators($el).done(function (errors) { 108 $el.data('bs.validator.errors', errors) 109 110 errors.length ? self.showErrors($el) : self.clearErrors($el) 111 112 if (!prevErrors || errors.toString() !== prevErrors.toString()) { 113 e = errors.length 114 ? $.Event('invalid.bs.validator', {relatedTarget: $el[0], detail: errors}) 115 : $.Event('valid.bs.validator', {relatedTarget: $el[0], detail: prevErrors}) 116 117 self.$element.trigger(e) 118 } 119 120 self.toggleSubmit() 121 122 self.$element.trigger($.Event('validated.bs.validator', {relatedTarget: $el[0]})) 123 }) 124 } 125 126 127 Validator.prototype.runValidators = function ($el) { 128 var errors = [] 129 var deferred = $.Deferred() 130 var options = this.options 131 132 $el.data('bs.validator.deferred') && $el.data('bs.validator.deferred').reject() 133 $el.data('bs.validator.deferred', deferred) 134 135 function getErrorMessage(key) { 136 return $el.data(key + '-error') 137 || $el.data('error') 138 || key == 'native' && $el[0].validationMessage 139 || options.errors[key] 140 } 141 142 $.each(Validator.VALIDATORS, $.proxy(function (key, validator) { 143 if (($el.data(key) || key == 'native') && !validator.call(this, $el)) { 144 var error = getErrorMessage(key) 145 !~errors.indexOf(error) && errors.push(error) 146 } 147 }, this)) 148 149 if (!errors.length && $el.val() && $el.data('remote')) { 150 this.defer($el, function () { 151 var data = {} 152 data[$el.attr('name')] = $el.val() 153 $.get($el.data('remote'), data) 154 .fail(function (jqXHR, textStatus, error) { errors.push(getErrorMessage('remote') || error) }) 155 .always(function () { deferred.resolve(errors)}) 156 }) 157 } else deferred.resolve(errors) 158 159 return deferred.promise() 160 } 161 162 Validator.prototype.validate = function () {
163 var delay = this.options.delay 164 165 this.options.delay = 0 166 this.$element.find(inputSelector).trigger('input.bs.validator') 167 this.options.delay = delay 168 169 return this 170 } 171 172 Validator.prototype.showErrors = function ($el) { 173 var method = this.options.html ? 'html' : 'text' 174 175 this.defer($el, function () { 176 var $group = $el.closest('.form-group') 177 var $block = $group.find('.help-block.with-errors') 178 var $feedback = $group.find('.form-control-feedback') 179 var errors = $el.data('bs.validator.errors') 180 181 if (!errors.length) return 182 183 errors = $('<ul/>') 184 .addClass('list-unstyled') 185 .append($.map(errors, function (error) { return $('<li/>')[method](error) })) 186 187 $block.data('bs.validator.originalContent') === undefined && $block.data('bs.validator.originalContent', $block.html()) 188 $block.empty().append(errors) 189 $group.addClass('has-error') 190 191 $feedback.length 192 && $feedback.removeClass(this.options.feedback.success) 193 && $feedback.addClass(this.options.feedback.error) 194 && $group.removeClass('has-success') 195 }) 196 } 197 198 Validator.prototype.clearErrors = function ($el) { 199 var $group = $el.closest('.form-group') 200 var $block = $group.find('.help-block.with-errors') 201 var $feedback = $group.find('.form-control-feedback') 202 203 $block.html($block.data('bs.validator.originalContent')) 204 $group.removeClass('has-error') 205 206 $feedback.length 207 && $feedback.removeClass(this.options.feedback.error) 208 && $feedback.addClass(this.options.feedback.success) 209 && $group.addClass('has-success') 210 } 211 212 Validator.prototype.hasErrors = function () { 213 function fieldErrors() { 214 return !!($(this).data('bs.validator.errors') || []).length 215 } 216 217 return !!this.$element.find(inputSelector).filter(fieldErrors).length 218 } 219 220 Validator.prototype.isIncomplete = function () { 221 function fieldIncomplete() { 222 return this.type === 'checkbox' ? !this.checked : 223 this.type === 'radio' ? !$('[name="' + this.name + '"]:checked').length : 224 $.trim(this.value) === '' 225 } 226 227 return !!this.$element.find(inputSelector).filter('[required]').filter(fieldIncomplete).length 228 } 229 230 Validator.prototype.onSubmit = function (e) { 231 this.validate() 232 if (this.isIncomplete() || this.hasErrors()) e.preventDefault() 233 } 234 235 Validator.prototype.toggleSubmit = function () { 236 if(!this.options.disable) return 237 var $btn = $('button[type="submit"], input[type="submit"]') 238 .filter('[form="' + this.$element.attr('id') + '"]') 239 .add(this.$element.find('input[type="submit"], button[type="submit"]')) 240 $btn.toggleClass('disabled', this.isIncomplete() || this.hasErrors()) 241 .css({'pointer-events': 'all', 'cursor': 'pointer'}) 242 } 243 244 Validator.prototype.defer = function ($el, callback) { 245 callback = $.proxy(callback, this) 246 if (!this.options.delay) return callback() 247 window.clearTimeout($el.data('bs.validator.timeout')) 248 $el.data('bs.validator.timeout', window.setTimeout(callback, this.options.delay)) 249 } 250 251 Validator.prototype.destroy = function () { 252 this.$element 253 .removeAttr('novalidate') 254 .removeData('bs.validator') 255 .off('.bs.validator') 256 257 this.$element.find(inputSelector) 258 .off('.bs.validator') 259 .removeData(['bs.validator.errors', 'bs.validator.deferred']) 260 .each(function () { 261 var $this = $(this) 262 var timeout = $this.data('bs.validator.timeout') 263 window.clearTimeout(timeout) && $this.removeData('bs.validator.timeout') 264 }) 265 266 this.$element.find('.help-block.with-errors').each(function () { 267 var $this = $(this) 268 var originalContent = $this.data('bs.validator.originalContent') 269 270 $this 271 .removeData('bs.validator.originalContent') 272 .html(originalContent) 273 }) 274 275 this.$element.find('input[type="submit"], button[type="submit"]').removeClass('disabled') 276 277 this.$element.find('.has-error').removeClass('has-error') 278 279 return this 280 } 281 282 // VALIDATOR PLUGIN DEFINITION 283 // =========================== 284 285 286 function Plugin(option) { 287 return this.each(function () { 288 var $this = $(this) 289 var options = $.extend({}, Validator.DEFAULTS, $this.data(), typeof option == 'object' && option) 290 var data = $this.data('bs.validator') 291 292 if (!data && option == 'destroy') return 293 if (!data) $this.data('bs.validator', (data = new Validator(this, options))) 294 if (typeof option == 'string') data[option]() 295 }) 296 } 297 298 var old = $.fn.validator 299 300 $.fn.validator = Plugin 301 $.fn.validator.Constructor = Validator 302 303 304 // VALIDATOR NO CONFLICT 305 // ===================== 306 307 $.fn.validator.noConflict = function () { 308 $.fn.validator = old 309 return this 310 } 311 312 313 // VALIDATOR DATA-API 314 // ================== 315 316 $(window).on('load', function () { 317 $('form[data-toggle="validator"]').each(function () { 318 var $form = $(this) 319 Plugin.call($form, $form.data()) 320 }) 321 }) 322 323}(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.