1(function (window, document) { 2 3 function PkValidation() { 4 var that = this; 5 var items = []; 6 var props = { 7 fieldErrorCssClass: "field-validation-error", 8 fieldValidCssClass: "field-validation-valid" , 9 inputErrorCssClass: "input-validation-error", 10 customFieldErrorCssClass: undefined, 11 customFieldValidCssClass: undefined, 12 customInputErrorCssClass: undefined 13 } 14 15 that.Init = function (form, params) { 16 params = params || {}; 17 props = { 18 fieldErrorCssClass: params.fieldErrorCssClass || props.fieldErrorCssClass, 19 fieldValidCssClass: params.fieldValidCssClass || props.fieldValidCssClass, 20 inputErrorCssClass: params.inputErrorCssClass || props.inputErrorCssClass, 21 customFieldErrorCssClass: params.customFieldErrorCssClass || props.customFieldErrorCssClass, 22 customFieldValidCssClass: params.customFieldValidCssClass || props.customFieldValidCssClass, 23 customInputErrorCssClass: params.customInputErrorCssClass || props.customInputErrorCssClass 24 } 25 var validationElements = form.querySelectorAll("[data-val='true']"); 26 for (var i = 0; i < validationElements.length; i++) { 27 var elm = validationElements[i]; 28 elm.setAttribute("novalidate", true); 29 elm.removeAttribute("required"); // Disable Ïο HTML5 based validation 30 var item = { 31 id: elm.id, 32 name: elm.getAttribute("name"), 33 type: elm.type, 34 required: elm.getAttribute("data-val-required"), 35 email: elm.getAttribute("data-val-email"), 36 date: elm.getAttribute("data-val-date"), 37 maxLength: elm.getAttribute("data-val-maxlength"), 38 maxLengthMax: elm.getAttribute("data-val-maxlength-max"), 39 minLength: elm.getAttribute("data-val-minlength"), 40 minLengthMin: elm.getAttribute("data-val-minlength-min"), 41 regex: elm.getAttribute("data-val-regex"), 42 regexPattern: elm.getAttribute("data-val-regex-pattern"), 43 range: elm.getAttribute("data-val-range"), 44 rangeMax: elm.getAttribute("data-val-range-max"), 45 rangeMin: elm.getAttribute("data-val-range-min"), 46 displayErrorElement: form.querySelector("[data-valmsg-for='" + elm.getAttribute("name") + "']"), 47 validate: ValidateItem 48 }; 49 elm.setAttribute("data-val-index", i); 50 elm.addEventListener("blur", function (e) { 51 if (e.keyCode != 9) { 52 var index = e.target.getAttribute("data-val-index"); 53 if (!index) return; 54 items[index].validate(); 55 } 56 }); 57 elm.addEventListener("keyup", function (e) { 58 if (e.keyCode != 9) { 59 var index = e.target.getAttribute("data-val-index"); 60 if (!index) return; 61 ClearValidationErrors(items[index]); 62 } 63 }); 64 items.push(item); 65 } 66 form.addEventListener("submit", function (e) { 67 if (!Validate()) { 68 e.preventDefault(); 69 } 70 }); 71 } 72 73 function Validate() { 74 var result = true; 75 var errorFound = false; 76 for (var i = 0; i < items.length; i++) { 77 var elm = document.getElementById(items[i].id); 78 result = items[i].validate() && result; 79 if (!errorFound && !result && elm.type !== "hidden" && !elm.disabled) { 80 elm.focus(); 81 elm.select(); 82 errorFound = true; 83 } 84 } 85 return result; 86 } 87 that.Validate = Validate; 88 89 function ClearValidationErrors(item) { 90 var elm = document.getElementById(item.id); 91 for (var i = 0; i < item.displayErrorElement.childNodes.length; i++) { 92 item.displayErrorElement.removeChild(item.displayErrorElement.childNodes[i]); 93 } 94 item.displayErrorElement.classList.remove(props.fieldErrorCssClass); 95 if (props.customFieldErrorCssClass) { 96 item.displayErrorElement.classList.remove(props.customFieldErrorCssClass); 97 } 98 item.displayErrorElement.classList.add(props.fieldValidCssClass); 99 if (props.customFieldValidCssClass) { 100 item.displayErrorElement.classList.add(props.customFieldValidCssClass); 101 } 102 elm.classList.remove(props.inputErrorCssClass); 103 if (props.customInputErrorCssClass) { 104 elm.classList.remove(props.customInputErrorCssClass); 105 } 106 } 107 108 function ShowFieldValidationError(item, txt) { 109 var elm = document.getElementById(item.id); 110 if (!item.displayErrorElement.classList.contains(props.fieldErrorCssClass)) { 111 item.displayErrorElement.appendChild(txt); 112 item.displayErrorElement.classList.remove(props.fieldValidCssClass); 113 if (props.customFieldValidCssClass) { 114 item.displayErrorElement.classList.remove(props.customFieldValidCssClass); 115 } 116 item.displayErrorElement.classList.add(props.fieldErrorCssClass); 117 if (props.customFieldErrorCssClass) { 118 item.displayErrorElement.classList.add(props.customFieldErrorCssClass); 119 } 120 } 121 if (!elm.classList.contains(props.inputErrorCssClass)) { 122 elm.classList.add(props.inputErrorCssClass); 123 if (props.customInputErrorCssClass) { 124 elm.classList.add(props.customInputErrorCssClass); 125 } 126 } 127 } 128 129 function ValidateItem(item) { 130 item = item || this; 131 var elm = document.getElementById(item.id); 132 // Required 133 if (item.required) { 134 if (!elm.value || elm.value === "") { 135 ShowFieldValidationError(item, document.createTextNode(item.required)); 136 return false; 137 } else if (item.type == "checkbox" && !elm.checked) { 138 ShowFieldValidationError(item, document.createTextNode(item.required)); 139 return false; 140 } else { 141 ClearValidationErrors(item); 142 } 143 } 144 // Email 145 if (item.email) { 146 var re = /^(([^<>()\[\]\\.,;:\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,}))$/; 147 if (!re.test(elm.value.toLowerCase())) { 148 ShowFieldValidationError(item, document.createTextNode(item.email)); 149 return false; 150 } else { 151 ClearValidationErrors(item); 152 } 153 } 154 // Date (dd/MM/yyyy) 155 if (item.date) { 156 var re = /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2}
156)$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/; 157 if (!re.test(elm.value.toLowerCase())) { 158 ShowFieldValidationError(item, document.createTextNode(item.date)); 159 return false; 160 } else { 161 ClearValidationErrors(item); 162 } 163 } 164 // MaxLength 165 if (item.maxLength && item.maxLengthMax && elm.value && elm.value.length > item.maxLengthMax) { 166 ShowFieldValidationError(item, document.createTextNode(item.maxLength)); 167 return false; 168 } else { 169 ClearValidationErrors(item); 170 } 171 // MinLength 172 if (item.minLength && item.minLengthMin && elm.value && elm.value.length < item.minLengthMin) { 173 ShowFieldValidationError(item, document.createTextNode(item.minLength)); 174 return false; 175 } else { 176 ClearValidationErrors(item); 177 } 178 // RegEx 179 if (item.regex && item.regexPattern && elm.value) { 180 var re = new RegExp(item.regexPattern); 181 if (!re.test(elm.value)) { 182 ShowFieldValidationError(item, document.createTextNode(item.regex)); 183 return false; 184 } else { 185 ClearValidationErrors(item); 186 } 187 } 188 // Range 189 if (item.range && item.rangeMax && item.rangeMin) { 190 if ((item.rangeMax && elm.value > item.rangeMax) || (item.rangeMin && elm.value < item.rangeMin)) { 191 ShowFieldValidationError(item, document.createTextNode(item.range)); 192 return false; 193 } else { 194 ClearValidationErrors(item); 195 } 196 } 197 return true; 198 } 199 } 200 201 window.PkValidation = new PkValidation(); 202 203 //InitializeValidation(document.querySelector(".form")); 204})(window, document);
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.