1 /* ========================================================================
2 * Bootstrap (plugin): validator.js v0.3.0
3 * ========================================================================
4 * The MIT License (MIT)
6 * Copyright (c) 2013 Spiceworks, Inc.
7 * Made by Cina Saffary (@1000hz) in the style of Bootstrap 3 era @fat
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
26 * ======================================================================== */
32 // VALIDATOR CLASS DEFINITION
33 // ==========================
35 var Validator = function (element, options) {
36 this.$element = $(element)
37 this.options = options
41 this.$element.on('input.bs.validator change.bs.validator focusout.bs.validator', $.proxy(this.validateInput, this))
43 this.$element.find('[data-match]').each(function () {
45 var target = $this.data('match')
47 $(target).on('input.bs.validator', function (e) {
48 $this.val() && $this.trigger('input')
53 Validator.DEFAULTS = {
56 match: 'Does not match',
57 minlength: 'Not long enough'
61 Validator.VALIDATORS = {
62 native: function ($el) {
64 return el.checkValidity ? el.checkValidity() : true
66 match: function ($el) {
67 var target = $el.data('match')
68 return !$el.val() || $el.val() === $(target).val()
70 minlength: function ($el) {
71 var minlength = $el.data('minlength')
72 return !$el.val() || $el.val().length >= minlength
76 Validator.prototype.validateInput = function (e) {
78 var prevErrors = $el.data('bs.errors')
81 this.$element.trigger(e = $.Event('validate.bs.validator', {relatedTarget: $el[0]}))
83 if (e.isDefaultPrevented()) return
85 $el.data('bs.errors', errors = this.runValidators($el))
87 errors.length ? this.showErrors($el) : this.clearErrors($el)
89 if (!prevErrors || errors.toString() !== prevErrors.toString()) {
91 ? $.Event('invalid.bs.validator', {relatedTarget: $el[0], detail: errors})
92 : $.Event('valid.bs.validator', {relatedTarget: $el[0], detail: prevErrors})
94 this.$element.trigger(e)
99 this.$element.trigger($.Event('validated.bs.validator', {relatedTarget: $el[0]}))
102 Validator.prototype.runValidators = function ($el) {
104 var validators = [Validator.VALIDATORS.native]
106 $.each(Validator.VALIDATORS, $.proxy(function (key, validator) {
107 if (($el.data(key) || key == 'native') && !validator.call(this, $el)) {
108 var error = $el.data(key + '-error')
110 || key == 'native' && $el[0].validationMessage
111 || this.options.errors[key]
113 !~errors.indexOf(error) && errors.push(error)
120 Validator.prototype.validate = function () {
121 var delay = this.options.delay
123 this.options.delay = 0
124 this.$element.find(':input').trigger('input')
125 this.options.delay = delay
130 Validator.prototype.showErrors = function ($el) {
131 function callback() {
132 var $group = $el.closest('.form-group')
133 var $block = $group.find('.help-block.with-errors')
134 var errors = $el.data('bs.errors')
136 if (!errors.length) return
139 .addClass('list-unstyled')
140 .append($.map(errors, function (error) { return $('<li/>').text(error) }))
142 $block.data('bs.originalContent') === undefined && $block.data('bs.originalContent', $block.html())
143 $block.empty().append(errors)
145 $group.addClass('has-error')
148 if (this.options.delay) {
149 window.clearTimeout($el.data('bs.timeout'))
150 $el.data('bs.timeout', window.setTimeout(callback, this.options.delay))
154 Validator.prototype.clearErrors = function ($el) {
155 var $group = $el.closest('.form-group')
156 var $block = $group.find('.help-block.with-errors')
158 $block.html($block.data('bs.originalContent'))
159 $group.removeClass('has-error')
162 Validator.prototype.hasErrors = function () {
163 function fieldErrors() {
164 return !!($(this).data('bs.errors') || []).length
167 return !!this.$element.find(':input').filter(fieldErrors).length
170 Validator.prototype.isIncomplete = function () {
171 function fieldIncomplete() {
172 return this.type === 'checkbox' ? !this.checked :
173 this.type === 'radio' ? !$('[name="' + this.name + '"]:checked').length :
174 $.trim(this.value) === ''
177 return !!this.$element.find('[required]').filter(fieldIncomplete).length
180 Validator.prototype.toggleSubmit = function () {
181 var $btn = this.$element.find(':submit')
182 $btn.attr('disabled', this.isIncomplete() || this.hasErrors())
186 // VALIDATOR PLUGIN DEFINITION
187 // ===========================
189 var old = $.fn.validator
191 $.fn.validator = function (option) {
192 return this.each(function () {
194 var options = $.extend({}, Validator.DEFAULTS, $this.data(), typeof option == 'object' && option)
195 var data = $this.data('bs.validator')
197 if (!data) $this.data('bs.validator', (data = new Validator(this, options)))
198 if (typeof option == 'string') data[option]()
202 $.fn.validator.Constructor = Validator;
205 // VALIDATOR NO CONFLICT
206 // =====================
208 $.fn.validator.noConflict = function () {
214 // VALIDATOR DATA-API
215 // ==================
217 $(window).on('load', function () {
218 $('form[data-toggle="validator"]').each(function () {
220 $form.validator($form.data())