1(function ( $ ) { 2 var main_options = { 3 'classes': { 4 'parent_class': 'form-group', 5 'parent_class_error': 'has-error', 6 'parent_class_validated': '', 7 'field_error_message': 'form-validation-error', 8 'field_error_message_js': 'js-form-validation-error' 9 }, 10 'seperate_icon': true, 11 'error_class_directed_at': 'self' 12 }; 13 14 $.fn.form_validation = function( validators, options ) { 15 var $this = this; 16 17 if (options && options != '') { 18 main_options = options; 19 } 20 var errors = []; 21 var input_value = $this.val(); 22 23 if ($this.is(':visible')) { 24 // check what validators are needed 25 $.each(validators, function (key, validator) { 26 if (validator.message == '') { 27 validator.message ='invalid ' + validator.type; 28 } 29 30 // REQUIRED Validator 31 if (validator.type == 'required') { 32 if (($this.attr('type') == 'radio' && !$('input[name="' + $this.attr('name') + '"]:checked').val()) || (input_value == '' || input_value == null)) { 33 errors.push(validator.message); 34 } 35 } 36 37 // if it's required then its empty, if its empty no other valiadation is needed, 38 // for it wil almost always fail the check 39 40 if (input_value != '' && input_value != null) { 41 // LENGTH Validator 42 if (validator.type == 'length') { 43 if (validator.max && input_value > validator.max) { 44 errors.max_length = 'length msg exceeds maximum'; 45 } else if (validator.min && input_value < validator.min) { 46 errors.push(validator.message); 47 } 48 } 49 50 // NUM_RANGE Validator 51 if (validator.type == 'num_range') { 52 if (input_value > validator.max && input_value < validator.min) { 53 errors.push(validator.message); 54 } 55 } 56 57 // EQUAL_TO Validator 58 if (validator.type == 'equal_to') { 59 var name = validator.name; // name can be anything the equal to is called for, could be equal_to or value. Needs to be checked later 60 var equal_to_value = $('[name=' + name + ']').val(); 61 if (input_value != equal_to_value) { 62 errors.push(validator.message); 63 } 64 } 65 66 // EMAIL Validator 67 if (validator.type == 'email') { 68 var regex = /^(([^<>()\[\]\\.,;:\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,}))$/; 69 var check = regex.exec(input_value); 70 if (!check) { 71 errors.push(validator.message); 72 } 73 } 74 75 // REGEXP Validator, does custom regexp check 76 if (validator.type == 'regexp') { 77 var regex = new RegExp(validator.pattern); 78 if (!regex.exec(input_value)) { 79 errors.push(validator.message); 80 } 81 } 82 83 // ZIPCODE Validator 84 if (validator.type == 'zipcode' || validator.type.indexOf('zip_check') != -1) { 85 input_value = input_value.replace(/ /g,''); 86 $this.val(input_value); 87 var regex = /^\d{4}[A-Z,a-z]{2}$/; 88 89 90 // check if country is the actual value to check on if not change it to whatever it is. 91 if ((validator.type == 'austrain_zip_check' || validator.type == 'belgium_zip_check') && parseInt(input_value) >= 1000 && parseInt(input_value) <= 9999) { 92 //do nothing 93 } else if (validator.type == 'dutch_zip_check' && regex.exec(input_value) && parseInt(input_value.substr(0, 4)) > 1000) { 94 //do nothing 95 } else if (validator.type == 'german_zip_check' && input_value.length >= 4 && input_value.length <= 5) { 96 //do nothing 97 } else if (validator.type == 'french_zip_check' && input_value.length == 5) { 98 //do nothing 99 } else { 100 errors.push(validator.message); 101 } 102 } 103 104 // PHONE Validator 105 if (validator.type == 'phone') { 106 if (validator.country == 'german') { 107 if (input_value.length < 8 || input_value.length > 20) { 108 //do nothing 109 } else { 110 errors.push(validator.message); 111 } 112 } 113 } 114 115 // DIGITS Validator 116 if (validator.type == 'digits') { 117 if (!isNaN(parseFloat(input_value)) && isFinite(input_value)) { 118 //do nothing 119 } else { 120 errors.push(validator.message); 121 } 122 } 123 124 /* TODO: needs to be defined what to do for date check */ 125 // DATE Validator 126 /*if (validator.type == 'date') { 127 var dateFormat = validator.format; 128 129 if (year > 1970 && date <= now) {
130 if ((month == 4 || month == 6 || month == 9 || month == 11) && day <= 30) { 131 alert("Date is valid"); 132 } else if (month == 2 && day <= 28) { 133 alert("Date is valid"); 134 } else if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day <= 31) { 135 alert("Date is valid"); 136 } else { 137 alert("Date is in-valid"); 138 } 139 } else { 140 alert("Date is in-valid"); 141 } 142 }*/ 143 } 144 }); 145 146 $this.show_validation_errors( errors, main_options ); 147 148 } else { 149 $this.clear_validation_errors( main_options ); 150 } 151 }; 152 153 $.fn.show_validation_errors = function ( errors, options ) { 154 var $this = this; 155 156 if (options && options != '') { 157 main_options = options; 158 } 159 160 if ($this.attr('type') == 'radio') { 161 var $error_container = $this.parents('.' + main_options.classes.parent_class).find('.' + main_options.classes.field_error_message_js); 162 } else { 163 var $error_container = $this.parent().find('.' + main_options.classes.field_error_message_js); 164 } 165 166 if ($error_container.length == 0) { 167 var $error_template = $('<div class="' + main_options.classes.field_error_message + ' ' + main_options.classes.field_error_message_js + '"/>'); 168 if ($this.attr('type') == 'radio') { 169 $this.parents('.' + main_options.classes.parent_class).find('.' + main_options.classes.field_error_message_js).remove(); 170 $this.parents('.' + main_options.classes.parent_class).append($error_template); 171 var $error_container = $this.parents('.' + main_options.classes.parent_class).find('.' + main_options.classes.field_error_message_js); 172 } else { 173 $error_template.insertAfter($this); 174 var $error_container = $this.parent().find('.' + main_options.classes.field_error_message_js); 175 } 176 177 } 178 179 if (main_options.seperate_icon) { 180 var $status_element = $this.parent().find('.js-status-element'); 181 if ($status_element.length == 0) { 182 var $status_template = $('<span class="js-status-element status-element fa"/>'); 183 if ($this.attr('type') == 'radio') { 184 $this.parents('.' + main_options.classes.parent_class).find('.js-status-element').remove(); 185 $this.parents('.' + main_options.classes.parent_class).addClass('is-relative is-radio').append($status_template); 186 var $status_element = $this.parents('.' + main_options.classes.parent_class).find('.js-status-element'); 187 188 } else { 189 $status_template.insertAfter($this); 190 $this.parent().addClass('is-relative'); 191 var $status_element = $this.parent().find('.js-status-element'); 192 } 193 } 194 } 195 196 var $element_for_error_class = $this; 197 if (main_options.error_class_directed_at == 'parent') { 198 $element_for_error_class = $this.parent(); 199 } 200 201 if (errors.length > 0) { 202 203 $element_for_error_class.addClass(main_options.classes.parent_class_error).removeClass(main_options.classes.parent_class_validated); 204 $error_container.empty(); 205 var html = $('<ul/>'); 206 $.each(errors, function (key, error) { 207 var li = $(' <li/>'); 208 li.html(error); 209 html.append(li); 210 }); 211 212 $error_container.html(html).show(); 213 if (main_options.seperate_icon) { 214 $status_element.addClass('fa-exclamation bad-state').removeClass('fa-check good-state'); 215 } 216 } else { 217 $element_for_error_class.removeClass(main_options.classes.parent_class_error).addClass(main_options.classes.parent_class_validated); 218 if (main_options.seperate_icon) { 219 $status_element.addClass('fa-check good-state').removeClass('fa-exclamation bad-state'); 220 if ($this.parents('.' + main_options.classes.parent_class).find('.js-status-element.bad-state').length == 0) { 221 $error_container.empty().remove(); 222 } 223 } else if (main_options.seperate_icon == false 224 && !$this.parents('.' + main_options.classes.parent_class).hasClass('.' + main_options.parent_class_error) 225 && !$this.parents('.' + main_options.classes.parent_class).hasClass('.' + main_options.parent_class_validated)) { 226 $error_container.empty().remove(); 227 } 228 } 229 }; 230
231 $.fn.clear_validation_errors = function ( options ) { 232 if (options && options != '') { 233 main_options = options; 234 } 235 236 var $element_for_error_class = $(this); 237 if (main_options.error_class_directed_at == 'parent') { 238 $element_for_error_class = $(this).parent(); 239 } 240 $element_for_error_class.removeClass(main_options.classes.parent_class_error).removeClass(main_options.classes.parent_class_validated); 241 $(this).parent().find('.' + main_options.classes.field_error_message_js).remove(); 242 $(this).parent().find('.js-status-element').remove(); 243 }; 244 245}( 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.