1function validateControl(controlId, isRequired, content, errorClass, errControlId) { 2 error = false; 3 $control = $('#' + controlId); 4 $errControlId = $control; 5 control_value = $control.val(); 6 control_value_length = 0; 7 if ($control.val() !== undefined && $control.val() !== null) { 8 control_value_length = $control.val().trim().length; 9 } 10 11 if (errControlId !== undefined) { 12 $errControlId = $('#' + errControlId); 13 } 14 15 if (isRequired) { 16 if (isRequired && control_value_length === 0) { 17 error = true; 18 markError($errControlId, errorClass); 19 } 20 else { 21 clearError($errControlId, errorClass); 22 } 23 } 24 else { 25 if (control_value_length === 0) { 26 return true; 27 } 28 } 29 30 if (content.length > 0) { 31 var functions = content.split(','); 32 for (var i = 0; i < functions.length; i++) { 33 var groups = functions[i].trim().match(/(\w+)(\(([^)]+)\))?/); // Delar upp i type och args utifrån mönstret: type(args) 34 var type = groups[1]; 35 var args = groups[3]; 36 37 switch (type.toLowerCase()) { 38 case "min": 39 if (control_value_length < args) { 40 error = true; 41 markError($errControlId, errorClass); 42 } 43 break; 44 case "max": 45 if (control_value_length > args) { 46 error = true; 47 markError($errControlId, errorClass); 48 } 49 break; 50 case "letter": 51 //if (!onlyContains(control_value, "[a-zåäöü]", args)) { // TODO: Utöka med att speca allt vi faktiskt tillåter 52 if (!onlyContains(control_value, "[^0-9!@#$%&*()_+=\\-|<>?{}
52\\[\\]~]", args)) { // Nu godkänner vi allt FÃRUTOM specade tecken 53 error = true; 54 markError($errControlId, errorClass); 55 } 56 break; 57 case "number": 58 if (control_value_length > 0) { 59 if (!onlyContains(control_value, "\\d", args)) { 60 error = true; 61 markError($errControlId, errorClass); 62 } 63 } 64 break; 65 case "numberpositive": 66 if (control_value_length > 0) { 67 const cleaned = control_value 68 .replace(/\s+/g, "") // remove spaces 69 .replace(/\./g, "") // remove thousand dots 70 .replace(/,/g, "."); // comma becomes decimal 71 const isNumeric = /^\d+(\.\d+)?$/.test(cleaned); 72 73 if (!isNumeric || parseFloat(cleaned) <= 0) { 74 error = true; 75 markError($errControlId, errorClass); 76 } 77 } 78 break; 79 case "numbernegative": 80 if (control_value_length > 0) { 81 const cleaned2 = control_value 82 .trim() 83 .replace(/\s+/g, "") 84 .replace(/\.(?=\d{3}\b)/g, "") 85 .replace(/,(?=\d{3}\b)/g, "") 86 .replace(",", "."); 87 88 const isNumeric2 = /^-?\d+(\.\d+)?$/.test(cleaned2); 89 90 if (!isNumeric2 || parseFloat(cleaned2) >= 0) { 91 error = true; 92 markError($errControlId, errorClass); 93 } 94 } 95 break; 96 case "email": 97 if (control_value_length > 0) { 98 if (!isValidEmail(control_value)) { 99 error = true; 100 markError($errControlId, errorClass); 101 } 102 } 103 break; 104 case "phone": 105 if (control_value_length > 0) { 106 if (!isValidPhoneNumber(control_value)) { 107 error = true; 108 markError($errControlId, errorClass); 109 } 110 } 111 break; 112 case "identitynumber": 113 case "personnummer": 114 if (control_value_length > 0) { 115 if (!isValidIdentityNumber(control_value)) { 116 error = true; 117 markError($errControlId, errorClass); 118 } 119 } 120 break; 121 case "date": 122 if (control_value_length > 0) { 123 if (isRequired && !isValidISODate(control_value)) { 124 error = true; 125 markError($errControlId, errorClass); 126 } 127 } 128 break; 129 default: 130 break; 131 } 132 } 133 } 134 135 if (error) { 136 return false; 137 } 138 139 return true; 140} 141 142function isValidISODate(isoDate) { 143 144 if (isNaN(Date.parse(isoDate))) { 145 return false; 146 } else { 147 if (isoDate !== (new Date(isoDate)).toISOString().substr(0, 10)) { 148 return false; 149 } 150 } 151 return true; 152} 153 154function ValidationOptions(control_id) { 155 this.validation_control_id = control_id; 156 this.validation_error_class = 'is-invalid'; 157 this.validation_hideTime = 2000; 158 159 var userLang = navigator.language || navigator.userLanguage; 160 if (userLang === 'sv-SE' || userLang === 'sv') { 161 this.content = 'Hoppsan, det verkar som du glömde nÃ¥got.'; 162 } 163 else { 164 //Fallback english 165 this.content = 'Oops, looks like you forgot something.'; 166 } 167 168 this.placement = 'top'; 169 this.trigger = 'click'; 170 this.delay = { show: 0, hide: 1000 }; 171} 172 173function showPopover(opts) { 174 popoverOptions = {}; 175 176 for (var prop in opts) { 177 if (opts.hasOwnProperty(prop) && !prop.startsWith('validation_')) { 178 popoverOptions[prop] = opts[prop]; 179 } 180 } 181 182 $('#' + opts.validation_control_id).popover(popoverOptions); 183 184 setTimeout(function () { 185 try { 186 $('#' + opts.validation_control_id).popover('dispose'); 187 } catch (e) { 188 // t.ex. om dispose inte finns i aktuell Bootstrap-version 189 $('#' + opts.validation_control_id).popover('destroy'); 190 } 191 }, opts.validation_hideTime); 192 193 $('#' + opts.validation_control_id).popover('show'); 194} 195 196/* Helper functions */ 197function markError(ctrlid, errorclass) { 198 $(ctrlid).addClass(errorclass); 199 200 $(ctrlid).focus(function() { 201 clearError(ctrlid, errorclass); 202 }); 203} 204 205function clearError(ctrlid, errorclass) { 206 $(ctrlid).removeClass(errorclass); 207} 208 209function isValidIdentityNumber(identityNumber) { 210 var matches = identityNumber.match(/^(\d{8})(-?)(\d{4})$/); 211 if (matches == null) { 212 return false; 213 } 214 identityNumber = "" + matches[1] + matches[3]; 215 var numbers = identityNumber.match(/^(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)$/); 216 var checkSum = 0; 217 var n; 218 var d = new Date(); 219 220 if (!isDate(identityNumber.substring(0, 4), identityNumber.substring(4, 6), identityNumber.substring(6, 8))) { 221 return false; 222 } 223 224 if (numbers == null) { return false; } 225 226 // Luhn 227 for (var i = 3; i <= 12; i++) { 228 n = parseInt(numbers[i]); 229 if (i % 2 === 0) { 230 checkSum += n; 231 } else {
232 checkSum += (n * 2) % 9 + Math.floor(n / 9) * 9; 233 } 234 } 235 236 if (checkSum % 10 === 0) { return true; } 237 return false; 238} 239 240 241function isValidEmail(email) { 242 // Regex from emailregex.com 243 return /^(([^<>()\[\]\\.,;:\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,}))$/.test(email); 244} 245 246function isValidPhoneNumber(phoneNumber) { 247 var onlyValidChars = /^[0-9+\-â ]+$/.test(phoneNumber); 248 var enoughNumbers = (phoneNumber.match(/[0-9]/g) || []).length >= 8; 249 return enoughNumbers && onlyValidChars; 250} 251 252function isDate(year, month, day) { 253 month = month - 1; 254 var tmpDate = new Date(year, month, day); 255 if ((year == tmpDate.getFullYear()) && 256 (month == tmpDate.getMonth()) && 257 (day == tmpDate.getDate())) { 258 return true; 259 } 260 else { 261 return false; 262 } 263} 264 265function onlyContains(value, pattern, extraChars) { 266 if (extraChars !== undefined) { 267 pattern = "(" + pattern + "|[" + extraChars.replace("-", "\\-") + "])"; 268 } 269 pattern = "^" + pattern + "*$"; 270 var regexp = new RegExp(pattern, "i"); 271 return regexp.test(value); 272}
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.