1class Validation { 2 /** 3 * Form validation. 4 * Check inputs based on the name attribute. 5 * 6 * - optionally, the input can have a data-required=1 7 * 8 * @param {jQuery} $form 9 * @param {jQuery} [$tips] - Element for displaying error messages 10 * @param {array} [requiredFields] - list of name attributes of inputs 11 * 12 * @returns {boolean} 13 */ 14 static validationFormInputs($form, $tips = null, requiredFields = []) { 15 if (!$form instanceof jQuery || ($tips && !$tips instanceof jQuery)) { 16 return false; 17 } 18 19 let inputs = $form.find('input[name]'); 20 21 let messages = ''; 22 let result = {}; 23 24 $(inputs).each(function () { 25 let $input = $(this); 26 let name = $input.attr('name'); 27 28 $input.removeClass('ui-state-error'); 29 30 if ($input.data('required') || contains(requiredFields, name)) { 31 result[`${name}` + 'NotEmpty'] = Validation.notEmpty($input, Validation.label(name)); 32 } 33 34 if ($input.val().length > 0) { 35 switch (name) { 36 case 'surname': 37 case 'building': 38 case 'street': 39 case 'town': 40 case 'name': 41 result[name + 'Lenght'] = Validation.length($input, Validation.label(name), 3, 50); 42 43 break; 44 case 'companyname': 45 result[name + 'Lenght'] = Validation.length($input, Validation.label(name), 3, 70); 46 47 break; 48 case 'postcode': 49 result[name + 'Lenght'] = Validation.length($input, Validation.label(name), 5, 10); 50 51 break; 52 case 'email': 53 result.emailLenght = Validation.length($input, Validation.label(name), 6, 150); 54 result.emailRegexp = Validation.regexp($input, 'A valid <b>Email Address</b> must be provided!', /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,); 55 56 break; 57 case 'password': 58 result.passwordLenght = Validation.length($input, Validation.label(name), 8, 30); 59 result.passwordRegexp = Validation.regexp($input, '<b>Password</b> must contain at least 8 characters, including UPPER/lowercase and numbers.', /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$/); 60 result.passwordConfirm = Validation.equality($input, $($form.find('input[name=password_confirm]:first')),'<b>Passwords</b> must match.'); 61 62 break; 63 case 'appliancetype': 64 result.appliancetypeLenght = Validation.length($input, Validation.label(name), 2, 30); 65 66 break; 67 case 'oilappliancemake': 68 result.oilappliancemakeLenght = Validation.length($input, Validation.label(name), 2, 50); 69 70 break; 71 // case 'gcn': 72 // result.gcnRegexp = Validation.regexp($input, 'Gas Council Number format is **-***-** where * is a number', '/^(\\d{2})[-\\/](\\d{3})[-\\/](\\d{2})$/'); 73 // 74 // break; 75 } 76 } 77 }); 78 79 Object.values(result).forEach(function (item) { 80 if (item !== true) { 81 if (messages) { 82 messages += '<br /><br />'; 83 } 84 85 messages += item; 86 } 87 }); 88 89 if (messages) { 90 if ($tips) { 91 Validation.updateTips($tips, messages); 92 } 93 94 return false;
95 } 96 97 return true; 98 } 99 100 /** 101 * Get label 102 * 103 * @param {string} name 104 * @returns {string} 105 */ 106 static label(name) { 107 const labels = { 108 password_confirm: 'Password Confirm', 109 appliancetype: 'Appliance Type', 110 oilappliancemake: 'Appliance Make', 111 companyname: 'Company Name' 112 }; 113 114 return labels[name] || ucfirst(name); 115 } 116 117 /** 118 * Display error messages 119 * 120 * @param {jQuery} $tips - Element for displaying error messages 121 * @param {html|string} [text] 122 */ 123 static updateTips($tips, text = null) { 124 if (!text) { 125 text = 'Unknown error.'; 126 } 127 128 $tips.html(text).removeClass('ui-state-highlight').fadeIn(); 129 130 setTimeout(function () { 131 $tips.addClass('ui-state-highlight'); 132 }, 1500); 133 134 setTimeout(function () { 135 $tips.fadeOut(3000); 136 }, 7000); 137 138 setTimeout(function () { 139 $tips.html(''); 140 }, 10000); 141 } 142 143 /** 144 * Tips display none and clear 145 * @param {jQuery} $tips 146 */ 147 static clearTips($tips) { 148 $tips.addClass('ui-state-highlight'); 149 $tips.fadeOut(); 150 $tips.html(''); 151 } 152 153 // Validation functions 154 /** 155 * Check for emptiness 156 * 157 * @param {jQuery} o 158 * @param {string} n - parameter name 159 * 160 * @returns {string|true} - error description or success 161 */ 162 static notEmpty(o, n) { 163 if (o.val().length === 0) { 164 o.addClass('ui-state-error'); 165 166 return `<b>${n}</b> must not be empty.`; 167 } 168 169 return true; 170 } 171 172 /** 173 * Checking for the number of characters 174 * 175 * @param {jQuery} o 176 * @param {string} n - parameter name 177 * @param {number} min 178 * @param {number} max 179 * 180 * @returns {string|true} - error description or success 181 */ 182 static length(o, n, min, max) { 183 let val = o.val().trim(); 184 185 if (val.length > max || val.length < min) { 186 o.addClass('ui-state-error'); 187 188 return `Length of <b>${n}</b> must be between <b>${min}</b> and <b>${max}</b>.`; 189 } 190 191 return true; 192 } 193 194 /** 195 * Checking against a regular expression 196 * 197 * @param {jQuery} o 198 * @param {string} msg - error description in case of failure 199 * @param regexp 200 * 201 * @returns {string|true} - error description or success 202 */ 203 static regexp(o, msg, regexp) { 204 if (!regexp.test(o.val())) { 205 o.addClass('ui-state-error'); 206 207 return msg; 208 } 209 210 return true; 211 } 212 213 /** 214 * Checking for equality of two values 215 * 216 * @param {jQuery} o1 217 * @param {jQuery} o2 218 * @param {string} msg - error description in case of failure 219 * 220 * @returns {string|true} - error description or success 221 */ 222 static equality(o1, o2, msg) { 223 if (o1.val() !== o2.val()) { 224 o1.addClass('ui-state-error'); 225 o2.addClass('ui-state-error'); 226 227 return msg; 228 } 229 230 return true; 231 } 232}
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.