|
| 1 | +// Job Application Form Validation and UI Feedback |
| 2 | +document.addEventListener("DOMContentLoaded", function () { |
| 3 | + const form = document.getElementById("jobAppForm"); |
| 4 | + const name = document.getElementById("name"); |
| 5 | + const email = document.getElementById("email"); |
| 6 | + const phone = document.getElementById("phone"); |
| 7 | + const password = document.getElementById("password"); |
| 8 | + const experience = document.getElementById("experience"); |
| 9 | + const salary = document.getElementById("salary"); |
| 10 | + const terms = document.getElementById("terms"); |
| 11 | + const fields = [name, email, phone, password, experience, salary]; |
| 12 | + |
| 13 | + // Focus highlighting |
| 14 | + fields.forEach((field) => { |
| 15 | + field.addEventListener("focus", function () { |
| 16 | + field.classList.add("focused"); |
| 17 | + }); |
| 18 | + field.addEventListener("blur", function () { |
| 19 | + // blur event is triggered when the field loses focus |
| 20 | + field.classList.remove("focused"); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + // Real-time validation feedback |
| 25 | + name.addEventListener("input", function () { |
| 26 | + validateName(); |
| 27 | + }); |
| 28 | + email.addEventListener("input", function () { |
| 29 | + validateEmail(); |
| 30 | + }); |
| 31 | + phone.addEventListener("input", function () { |
| 32 | + validatePhone(); |
| 33 | + }); |
| 34 | + password.addEventListener("input", function () { |
| 35 | + validatePassword(); |
| 36 | + }); |
| 37 | + experience.addEventListener("change", function () { |
| 38 | + validateExperience(); |
| 39 | + }); |
| 40 | + salary.addEventListener("input", function () { |
| 41 | + validateSalary(); |
| 42 | + }); |
| 43 | + |
| 44 | + // Validation functions |
| 45 | + /// name input validation |
| 46 | + function validateName() { |
| 47 | + const val = name.value.trim(); |
| 48 | + if (/^[A-Za-z ]{3,}$/.test(val)) { |
| 49 | + // .test(val) used to check if the value matches the regex pattern |
| 50 | + setValid(name); |
| 51 | + // setValid() is used to apply the valid class |
| 52 | + return true; |
| 53 | + } else { |
| 54 | + setInvalid(name); |
| 55 | + // setInvalid() is used to apply the invalid class |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// email input validation |
| 61 | + function validateEmail() { |
| 62 | + const val = email.value.trim(); |
| 63 | + if (/^[\w.-]+@[\w-]+\.[A-Za-z]{2,}$/.test(val)) { |
| 64 | + setValid(email); |
| 65 | + return true; |
| 66 | + } else { |
| 67 | + setInvalid(email); |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// phone input validation |
| 73 | + function validatePhone() { |
| 74 | + const val = phone.value.trim(); |
| 75 | + if (/^\d{11}$/.test(val)) { |
| 76 | + setValid(phone); |
| 77 | + return true; |
| 78 | + } else { |
| 79 | + setInvalid(phone); |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// password input validation |
| 85 | + function validatePassword() { |
| 86 | + const val = password.value; |
| 87 | + if (/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/.test(val)) { |
| 88 | + setValid(password); |
| 89 | + return true; |
| 90 | + } else { |
| 91 | + setInvalid(password); |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// experience input validation |
| 97 | + function validateExperience() { |
| 98 | + if (experience.value !== "") { |
| 99 | + setValid(experience); |
| 100 | + return true; |
| 101 | + } else { |
| 102 | + setInvalid(experience); |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /// salary input validation |
| 108 | + function validateSalary() { |
| 109 | + const val = parseFloat(salary.value); |
| 110 | + if (!isNaN(val) && val > 0) { |
| 111 | + setValid(salary); |
| 112 | + return true; |
| 113 | + } else { |
| 114 | + setInvalid(salary); |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // HelperFunctions to set valid/invalid classes |
| 120 | + function setValid(field) { |
| 121 | + field.classList.remove("invalid"); |
| 122 | + field.classList.add("valid"); |
| 123 | + } |
| 124 | + function setInvalid(field) { |
| 125 | + field.classList.remove("valid"); |
| 126 | + field.classList.add("invalid"); |
| 127 | + } |
| 128 | + |
| 129 | + // Form submit validation |
| 130 | + form.addEventListener("submit", function (e) { |
| 131 | + let errors = []; |
| 132 | + if (!validateName()) |
| 133 | + errors.push("Name must be at least 3 letters (letters only)."); |
| 134 | + if (!validateEmail()) errors.push("Enter a valid email address."); |
| 135 | + if (!validatePhone()) errors.push("Phone must be exactly 11 digits."); |
| 136 | + if (!validatePassword()) |
| 137 | + errors.push( |
| 138 | + "Password must be at least 8 characters, include uppercase, lowercase, and a digit." |
| 139 | + ); |
| 140 | + if (!validateExperience()) errors.push("Select an experience level."); |
| 141 | + if (!validateSalary()) |
| 142 | + errors.push("Expected salary must be a positive number."); |
| 143 | + if (!terms.checked) |
| 144 | + errors.push("You must agree to the terms & conditions."); |
| 145 | + |
| 146 | + if (errors.length > 0) { |
| 147 | + alert(errors.join("\n")); |
| 148 | + e.preventDefault(); |
| 149 | + } |
| 150 | + }); |
| 151 | +}); |
0 commit comments