|
| 1 | +/** |
| 2 | +* Bootstrap.validator plugin for JQuery and Bootstrap 3. |
| 3 | +* https://github.com/wpic/bootstrap.validator.js |
| 4 | +* By Hamed Abdollahpour 2014 - WPIC Co |
| 5 | +*/ |
| 6 | +(function ( $ ) { |
| 7 | + |
| 8 | + $.fn.validate = function( options ) { |
| 9 | + |
| 10 | + var settings = $.extend({ |
| 11 | + init: function() {}, /* init even */ |
| 12 | + success: function() {}, /* sucess even */ |
| 13 | + fail: function(invalids) {} /* Fail even */ |
| 14 | + /* join: ' ' */ /* Join array field data with it - If it be empty it will still remain array */ |
| 15 | + }, options ); |
| 16 | + |
| 17 | + var validators = { |
| 18 | + 'name+family': /^((?![0-9]).+\s.+)/g, |
| 19 | + 'name': /^((?![0-9]).+)/g, |
| 20 | + 'family': /^((?![0-9]).+)/g, |
| 21 | + 'number': /^[0-9]+/g, |
| 22 | + 'url': /^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/, |
| 23 | + 'date': /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/, |
| 24 | + 'email': /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, |
| 25 | + 'tel': /^[0-9\-\+]{3,25}$/ |
| 26 | + }; |
| 27 | + |
| 28 | + return this.on('submit', function(e) { |
| 29 | + var form = this; |
| 30 | + var invalids = []; |
| 31 | + |
| 32 | + settings.init.call(form); |
| 33 | + |
| 34 | + var data = {}; |
| 35 | + $('input,textarea,select', this).filter(":visible,[type='hidden']").each(function() { |
| 36 | + var i = $(this); |
| 37 | + if(!i.parent().is(':visible')) { |
| 38 | + return; |
| 39 | + } |
| 40 | + var name = i.attr('name'); |
| 41 | + if(typeof(name) != 'undefined' && (i.is(":not([type='checkbox'],[type='radio'])") || i.is(":checked"))) { |
| 42 | + var value = i.val(); |
| 43 | + |
| 44 | + if(typeof(data[name]) != 'undefined') { |
| 45 | + data[name] = [].concat(data[name]).concat(value); |
| 46 | + } else { |
| 47 | + data[name] = value; |
| 48 | + } |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + /* join array if it's required */ |
| 53 | + if (typeof(settings.join) != 'undefined') { |
| 54 | + for(var i in data) { |
| 55 | + if (typeof(data[i]) == "function") { |
| 56 | + data[i] = data[i].join(settings.join); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** [data-require] is deprecated use requried instead **/ |
| 62 | + $("[data-regex],[data-require],[data-required],[required],[data-equals]", form).filter(':visible').each(function() { |
| 63 | + var self = $(this); |
| 64 | + |
| 65 | + var regex = self.attr('data-regex'); |
| 66 | + var required = self.is('[required]') || self.is('[data-required]') /* the rest deprecated */ || self.is('[data-require]'); |
| 67 | + var equals = self.attr('data-equals'); |
| 68 | + var value = self.val(); |
| 69 | + |
| 70 | + if(self.is("[type='checkbox']") && !self.is(":checked")) value=''; |
| 71 | + |
| 72 | + // replace value with total value |
| 73 | + var name = self.attr('name'); |
| 74 | + if(typeof(name) != 'undefined') { |
| 75 | + value = data[name]; |
| 76 | + } |
| 77 | + |
| 78 | + if(typeof(equals) != 'undefined' && typeof(data[equals]) != 'undefined') { |
| 79 | + if(value != data[equals]) { |
| 80 | + invalids.push(this); |
| 81 | + invalids.push($("[name='" + equals + "']")[0]); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** array is always valid, because for array inputs we just check required **/ |
| 86 | + if(!Array.isArray(value)) { |
| 87 | + if(value && value.length > 0) { |
| 88 | + // try to use tag name for regex |
| 89 | + if (typeof(regex) == 'undefined') { |
| 90 | + var type = $(this).attr('type'); |
| 91 | + if (typeof(type) != 'undefined' && typeof(validators[type.toLowerCase()]) != 'undefined') { |
| 92 | + regex = type.toLowerCase(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (regex) { |
| 97 | + var r; |
| 98 | + if (typeof(validators[regex]) != 'undefined') { |
| 99 | + r = validators[regex] |
| 100 | + } |
| 101 | + // Use regex itself |
| 102 | + else { |
| 103 | + r = new RegExp(regex); |
| 104 | + } |
| 105 | + |
| 106 | + if(!r.test(value)) { |
| 107 | + invalids.push(this); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + else if(required) { |
| 112 | + invalids.push(this); |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + if(invalids.length > 0) { |
| 118 | + e.preventDefault(); |
| 119 | + settings.fail.call(form, e, invalids, data); |
| 120 | + } else { |
| 121 | + settings.success.call(form, e, data); |
| 122 | + } |
| 123 | + }) |
| 124 | + // Disable default HTML5 validation |
| 125 | + .attr('novalidate', ''); |
| 126 | + |
| 127 | + }; |
| 128 | + |
| 129 | + $.fn.bootstrap3Validate = function(success, data) { |
| 130 | + return this.validate({ |
| 131 | + 'init': function() { |
| 132 | + $('.has-error', this).removeClass('has-error').find('input,textarea').tooltip('destroy'); |
| 133 | + $('.alert').hide(); |
| 134 | + $("[role='tooltip']", this).tooltip('destroy'); |
| 135 | + }, |
| 136 | + 'success': function(e, data) { |
| 137 | + if (typeof(success) === 'function') { |
| 138 | + success.call(this, e, data); |
| 139 | + } |
| 140 | + }, |
| 141 | + 'fail': function(e, invalids) { |
| 142 | + var form = this; |
| 143 | + |
| 144 | + $(invalids).closest('.form-group').addClass('has-error').find('input,select,textarea').each(function(i) { |
| 145 | + var target = $(this); |
| 146 | + var text = target.attr('data-title'); |
| 147 | + if(!text) { |
| 148 | + text = target.attr('placeholder'); |
| 149 | + } |
| 150 | + |
| 151 | + if(text) { |
| 152 | + if(!target.is("[type='checkbox']")) { |
| 153 | + target.tooltip({'trigger':'focus', placement: 'top', title: text}); |
| 154 | + } |
| 155 | + |
| 156 | + if(i == 0) { |
| 157 | + $('.alert-danger', form).show().text(text); |
| 158 | + this.focus(); |
| 159 | + } |
| 160 | + } |
| 161 | + }); |
| 162 | + }, |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | +}( jQuery )); |
0 commit comments