1/** 2 * æ ¡éªè¡¨ååæ®µæ¯å¦ç¬¦åè¦æ± 3 * @param {Array|Object} validationform æ ¡éªè§åï¼æ ¼å¼ä¸º 4 * [ 5 { 6 required?:bloolean, é»è®¤ä¸ºtrue 7 key:string, å¿ å¡«-åæ®µå 8 value:string, å¿ å¡«-åæ®µå¼ 9 message?:string, é误æç¤º çä»·äº errorMessage.required 10 maxLength?:number, æå¤§é¿åº¦ 11 validate?:function, èªå®ä¹æ ¡éªå½æ° 12 regex?:string, æ£åè¡¨è¾¾å¼ 13 invalidWords?:Array ä¸å 许åºç°çå符 14 errorMessage?:Object é误æç¤º 15 } 16 ... 17 ] 18 * @returns {Object} æ ¡éªç»æå¯¹è±¡ï¼æ ¼å¼ä¸º 19 { 20 isValid: boolean, æ ¡éªæ¯å¦éè¿ 21 message: string, é误æç¤º 22 values: Object, åæ®µå¼ 23 } 24 */ 25 function validateForm(validationform) { 26 const valValid = { 27 isValid: true, 28 errorKey: null, // ð æ°å¢ 29 values: {}, 30 } 31 if(!Array.isArray(validationform)){ 32 validationform = [validationform]; 33 } 34 // ä¸å 许åºç°çå符 35 const disallowedChars =[ 36 "http", 37 "https", 38 "loan", 39 "loans", 40 "debt", 41 "debts", 42 "essay", 43 "essays", 44 "???", 45 ] 46 47 // åºå®æ ¡éªå段 48 const validationRules = [ 49 { 50 key: "product", 51 maxLength: 200, 52 invalidWords: disallowedChars, 53 required: true, 54 errorMessage: { 55 required: TextPrompt.productName, 56 maxLength: TextPrompt.productLength, 57 invalidWords: TextPrompt.productInvalidWord, 58 }, 59 }, 60 { 61 key: "username", 62 maxLength: 50, 63 invalidWords: disallowedChars, 64 required: true, 65 errorMessage: { 66 required: TextPrompt.name, 67 maxLength: TextPrompt.nameLength, 68 invalidWords: TextPrompt.nameInvalidWord, 69 }, 70 }, 71 { 72 key: "phone", 73 maxLength: 50, 74 regex: /^[0-9+\-\(\))]{1,50}$/, 75 required: true, 76 errorMessage: { 77 required: TextPrompt.phone, 78 invalid: TextPrompt.phoneLength, 79 }, 80 }, 81 { 82 key: "email", 83 maxLength: 100, 84 regex: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, 85 required: true, 86 errorMessage: { 87 required: TextPrompt.yourEmail, 88 maxLength: TextPrompt.emailLength, 89 invalid: TextPrompt.emailInvalidWord, 90 }, 91 }, 92 { 93 key: "message", 94 maxLength: 500, 95 required: true, 96 errorMessage: { 97 required: TextPrompt.comment, 98 maxLength: TextPrompt.commentsLength, 99 }, 100 }, 101 ]; 102 103 validationform.forEach(v=>{ 104 105 }) 106 for (const index in validationform) { 107 // required é»è®¤ä¸ºtrue 108 if (!("required" in validationform[index])) { 109 validationform[index].required = true; 110 } 111 let rule = validationRules.find( 112 (rule) => rule.key === validationform[index].key 113 ); 114 if (rule) { 115 validationform[index] = { ...rule, ...validationform[index] }; 116 } 117 if (validationform[index].message) { 118 if (!validationform[index].errorMessage) { 119 validationform[index].errorMessage = {}; 120 } 121 validationform[index].errorMessage.required = 122 validationform[index].message; 123 } 124 } 125 126 for (const [index, rule] of Object.entries(validationform)) { 127 const value = (rule.value+"" || "").trim(); 128 const errorMessage = rule.errorMessage; 129 if (rule.required && !value) { 130 valValid.message = errorMessage.required; 131 valValid.errorKey = rule.key 132 valValid.isValid = false; 133 } else if (!rule.required && !value) { 134 // 妿䏿¯å¿ 填项ä¸å¼ä¸ºç©ºï¼åè·³è¿æææ ¡éª 135 continue; 136 } else if (rule.maxLength && value.length > rule.maxLength) { 137 valValid.message = errorMessage.maxLength; 138 valValid.errorKey = rule.key 139 valValid.isValid = false; 140 }
140 else if (rule.invalidWords && containsInvalidWords(value, rule.invalidWords)) { 141 valValid.message = errorMessage.invalidWords; 142 valValid.errorKey = rule.key 143 valValid.isValid = false; 144 } else if (rule.regex && !rule.regex.test(value)) { 145 valValid.message = errorMessage.invalid; 146 valValid.errorKey = rule.key 147 valValid.isValid = false; 148 } else if (typeof rule.validate == "function" && !rule.validate(value)) { 149 let { validate, required } = rule.errorMessage; 150 valValid.message = validate || required; 151 valValid.errorKey = rule.key 152 valValid.isValid = false; 153 } 154 valValid.values[rule.key] = value; 155 if(!valValid.isValid){ 156 break; 157 } 158 } 159 160 return valValid; 161 } 162 163 // è¾ å©å½æ°ï¼æ£æ¥åç¬¦ä¸²ä¸æ¯å¦å 嫿 æåè¯ 164 function containsInvalidWords( 165 str, 166 invalidWords 167 ) { 168 return invalidWords.some((word) => str.toLowerCase().includes(word)); 169 } 170 171 // è¾ å©å½æ°ï¼æ ¡éªçµè¯å·ç 172 function isValidPhone(phone) { 173 const phoneRegex = /^[0-9+\-$$]{1,50}$/; 174 return phoneRegex.test(phone); 175 } 176 177 // è¾ å©å½æ°ï¼æ ¡éªçµåé®ä»¶å°å 178 function isValidEmail(email) { 179 const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; 180 return emailRegex.test(email); 181 } 182 183 184 function showToast(msg, type = "success") { 185 let toastHtml = ""; 186 if (type == "success") { 187 toastHtml = `<div class="oc-toast oc-toast-success"> 188 ${msg} 189 </div>`; 190 } 191 // å° toast æ·»å å°é¡µé¢ 192 $("body").append(toastHtml); 193 194 // è·åæ°æ·»å ç toast å ç´ 195 const $toast = $(".oc-toast").last(); 196 197 // 3 ç§åå é¤ toast 198 setTimeout(function () { 199 $toast.fadeOut(300, function () { 200 $(this).remove(); 201 }); 202 }, 3000); 203} 204 205/** 206 * è·åGoogle Recaptcha éªè¯ç 207 * @returns Google Recaptcha 208 */ 209function getGoogleSafeResponse() { 210 let response = ""; 211 try { 212 // å°è¯è·å第ä¸ä¸ªï¼å¦ææ²¡æå¼ï¼å°è¯è·å第äºä¸ª 213 // 注æï¼åªæå½ 0 åå¨ä¸æå¼æ¶ï¼æä¼è¿åï¼å¦åå°è¯ 1 214 response = grecaptcha.getResponse(0) || grecaptcha.getResponse(1); 215 } catch (e) { 216 // å¦æç´¢å¼ 1 ä¸åå¨ï¼æè·éè¯¯é²æ¢ç¨åºå´©æº 217 try { 218 response = grecaptcha.getResponse(0); 219 } catch (e2) { 220 response = ""; 221 } 222 } 223 return response; 224}
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.