1var MyValidation = (function($, Drupal) { 2 var self = this; 3 4 self.validate = function($container, parent) { 5 var valid = true; 6 //Validate empty 7 $container.find('input[required], select[required], textarea[required]').each(function() { 8 var $this = $(this); 9 10 if ($this.val() === '') { 11 $this.parents(parent).addClass('has-error error-empty'); 12 valid = false; 13 14 } else if ($this.parents(parent).find('input.needed').length !== 0) { 15 var vide = false; 16 17 $this.parents(parent).find('.needed').each(function() { 18 if ($(this).val() === '') { 19 vide = true; 20 } 21 }); 22 23 if (vide) { 24 $this.parents(parent).addClass('has-error error-empty'); 25 valid = false; 26 } else { 27 $this.parents(parent).removeClass('has-error error-empty'); 28 } 29 } else { 30 $this.parents(parent).removeClass('has-error error-empty'); 31 } 32 }); 33 34 //Validate checkbox 35 $container.find('.checkbox.required').each(function() { 36 var $this = $(this); 37 38 if ($('input[name="' + $this.attr('name') + '"]').is(':checked')) { 39 $this.parents(parent).removeClass('has-error error-checkbox'); 40 } else { 41 $this.parents(parent).addClass('has-error error-checkbox'); 42 valid = false; 43 } 44 }); 45 46 //Validate multiple checkbox 47 $container.find('.multiple-cbox.required').each(function() { 48 var $this = $(this), 49 checkboxes = $this.closest('form').find('input[type="checkbox"]').serializeArray(); 50 51 if (checkboxes.length !== 0) { 52 $this.parents(parent).removeClass('has-error error-multiple-cbox'); 53 } else { 54 $this.parents(parent).addClass('has-error error-multiple-cbox'); 55 valid = false; 56 } 57 }); 58 59 //Validate radio button 60 $container.find('.radio.required').each(function() { 61 var $this = $(this), 62 $input_name = $this.attr('name'); 63 64 if ($('input[name="' + $input_name + '"]').is(':checked')) { 65 $(this).parents(parent).removeClass('has-error error-radio'); 66 } else { 67 $(this).parents(parent).addClass('has-error error-radio'); 68 valid = false; 69 } 70 }); 71 72// if(valid){ 73 //Validate minLength 74 $container.find('.minLength').each(function() { 75 var $this = $(this); 76 77 if (!self.validator.minLength($this.val(), $this.data('limit'))) { 78 valid = false; 79 $(this).parents(parent).addClass('has-error error-length'); 80 } else { 81 $(this).parents(parent).removeClass('has-error error-length'); 82 } 83 }); 84 85 //Validate maxLength 86 $container.find('.maxLength').each(function() { 87 var $this = $(this); 88 89 if (!self.validator.maxLength($this.val(), $this.data('limit'))) { 90 valid = false; 91 $(this).parents(parent).addClass('has-error error-length'); 92 } else { 93 $(this).parents(parent).removeClass('has-error error-length'); 94 } 95 }); 96 97 //Validate email 98 $container.find('input.validEmail').each(function() { 99 var $this = $(this); 100 101 if (!self.validator.email($this.val())) { 102 $this.parents(parent).addClass('has-error error-email'); 103 valid = false; 104 } else { 105 $this.parents(parent).removeClass('has-error error-email'); 106 } 107 }); 108 109 //Validate email confirm 110 $container.find('input.validEmailConfirm').each(function() { 111 var $this = $(this); 112 var email = $('input.validEmail').val(); 113 114 if ($this.val() != '') { 115 if (!self.validator.equal(email, $this.val())) { 116 $this.parents(parent).addClass('has-error error-email-confirm'); 117 valid = false; 118 } else { 119 $this.parents(parent).removeClass('has-error error-email-confirm'); 120 } 121 } 122 }); 123 124 //Validate phone 125 $container.find('input.validPhone').each(function() { 126 var $this = $(this), 127 telephone = ''; 128 129 $this.parent().find('input').each(function() { 130 if ($(this).attr('name').indexOf('ext') == -1) { 131 telephone += $(this).val(); 132 } 133 }); 134
135 var validPhone = (telephone.length >= 7); 136 137 if (!validPhone) { 138 $(this).parents(parent).addClass('has-error error-telephone'); 139 valid = false; 140 } else { 141 $(this).parents(parent).removeClass('has-error error-telephone'); 142 } 143 }); 144 145 //Validate Canada postal code 146 $container.find('input.zipcode-ca').each(function() { 147 var $this = $(this); 148 149 if (!self.validator.cazip($this.val())) { 150 $this.parents(parent).addClass('has-error error-postalCode'); 151 valid = false; 152 } else { 153 $this.parents(parent).removeClass('has-error error-postalCode'); 154 } 155 }); 156 157 //Validate USA zipcode 158 $container.find('input.zipcode-us').each(function() { 159 var $this = $(this); 160 161 if ($this.val() != '') { 162 if (!self.validator.uszip($this.val())) { 163 $this.parents(parent).addClass('has-error error-postalCode'); 164 valid = false; 165 } else { 166 $this.parents(parent).removeClass('has-error error-postalCode'); 167 } 168 } 169 }); 170 171 //Validate mirror fields 172 if (valid) { 173 $container.find('input.same').each(function() { 174 var $this = $(this); 175 176 if ($this.attr('id').indexOf('_confirm') != -1) { 177 comparison_field = $this.attr('id').replace('_confirm', ''); 178 } else { 179 comparison_field = $this.attr('id') + '_confirm'; 180 } 181 if ($this.val() != $('#' + comparison_field).val() && $('#' + comparison_field).val() != '') { 182 $this.parents(parent).addClass('has-error error-same'); 183 $('#' + comparison_field).parents(parent).addClass('has-error error-same'); 184 valid = false; 185 } else { 186 $this.parents(parent).removeClass('has-error error-same'); 187 $('#' + comparison_field).parents(parent).removeClass('has-error error-same'); 188 } 189 }); 190 } 191// } 192 193 return valid; 194 }; 195 196 self.onPhoneKeyDown = function(e) { 197 // Allow only backspace, delete, tab, left , right 198 if ((e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105) && (e.keyCode != 46 && e.keyCode != 8 && e.keyCode != 9 && e.keyCode != 37 && e.keyCode != 39 && e.keyCode != 109)) { 199 e.preventDefault(); 200 } 201 }; 202 203 /** 204 * REGEX VALIDATIONS FUNTIONS 205 */ 206 207 self.validator = { 208 USPhone: function(val) { 209 return /^\(?(\d{3})\)?[\- ]?\d{3}[\- ]?\d{4}$/.test(val); 210 }, 211 212 uszip: function(val) { 213 return /^\d{5}(?:[-\s]\d{4})?$/.test(val); 214 }, 215 cazip: function(val) { 216 return /^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]\d[A-Za-z][ ]?\d[A-Za-z]\d$/.test(val); 217 }, 218 219 // matches mm/dd/yyyy (requires leading 0's (which may be a bit silly, what do you think?) 220 date: function(val) { 221 return /^(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12][0-9]|3[01])\/(?:\d{4})/.test(val); 222 }, 223 224 email: function(val) { 225 return /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/.test(val); 226 }, 227 228 minLength: function(val, length) { 229 return val.length >= length; 230 }, 231 232 maxLength: function(val, length) { 233 return val.length <= length; 234 }, 235 236 equal: function(val1, val2) { 237 return (val1 === val2); 238 } 239 }; 240 241 /** ------------------------------ 242 * Constructor 243 --------------------------------- */ 244 245 var construct = (function() { 246 247 })(); 248 249 return self; 250});
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.