@@ -20,7 +20,6 @@ import "../../styles/loader.css";
2020 maxlength: " 100" ,
2121 placeholder: formContent .frontmatter .fullname ,
2222 required: true ,
23- minlength: 2 ,
2423 }}
2524 labelProps ={ {
2625 for: " form-full-name" ,
@@ -103,7 +102,9 @@ import "../../styles/loader.css";
103102 "get-your-plan-section",
104103 );
105104
106- const form = document.getElementById("get-your-plan-form");
105+ const form = document.getElementById(
106+ "get-your-plan-form",
107+ ) as HTMLFormElement | null;
107108 /* Page form elements */
108109 const submitBtn = form?.querySelector("#form-submit-button");
109110 const submitBtnElems = submitBtn?.querySelectorAll("*");
@@ -129,8 +130,21 @@ import "../../styles/loader.css";
129130 const modalFormLoader =
130131 modalFormSubmitBtn?.querySelector("#modal-form-loader");
131132
132- const handleCloseModal = () => {
133- modalForm?.close();
133+ const handleValidation = (
134+ input: HTMLInputElement | null,
135+ errorMessage: string,
136+ form: HTMLFormElement | null,
137+ ) => {
138+ if (!input) return false;
139+
140+ const label = form?.querySelector<HTMLLabelElement>(
141+ `label[for="${input.id}"]`,
142+ );
143+ const placeholderSpan = label?.querySelector("span:first-child");
144+ const errorSpan = label?.querySelector("span:nth-child(2)");
145+ placeholderSpan?.classList.add("hidden");
146+ errorSpan?.classList.add("!inline", "text-red-500");
147+ if (errorSpan) errorSpan.textContent = errorMessage;
134148 };
135149
136150 const showModalLoader = () => {
@@ -150,7 +164,38 @@ import "../../styles/loader.css";
150164 submitBtnLoader?.classList.remove("!block");
151165 };
152166
153- if (form) {
167+ const handleFieldReset = (input: HTMLInputElement | null) => {
168+ if (!input) return;
169+
170+ const spanErr = form?.querySelector(`#error-text-value-${input.id}`);
171+ const placeholderSpan = form?.querySelector(`#label-value-${input.id}`);
172+
173+ input.addEventListener("click", () => {
174+ placeholderSpan?.classList.remove("hidden");
175+ spanErr?.classList.remove("!inline", "text-red-500");
176+ });
177+ };
178+
179+ const handleConsentReset = (checkbox: HTMLInputElement | null) => {
180+ if (!checkbox) return;
181+
182+ checkbox.addEventListener("click", () => {
183+ checkbox.classList.replace("border-red-600", "border-gray-300");
184+ checkbox.classList.remove("border-2");
185+ });
186+ };
187+
188+ const emailInput = form?.querySelector<HTMLInputElement>("#form-email");
189+ const nameInput =
190+ form?.querySelector<HTMLInputElement>("#form-full-name");
191+ const consentInput =
192+ form?.querySelector<HTMLInputElement>("#form-consent");
193+
194+ if (form && emailInput && nameInput && consentInput) {
195+ handleFieldReset(nameInput);
196+ handleFieldReset(emailInput);
197+ handleConsentReset(consentInput);
198+
154199 form.addEventListener("submit", async (e) => {
155200 e.preventDefault();
156201 const formData = new FormData(e.target as HTMLFormElement);
@@ -161,21 +206,46 @@ import "../../styles/loader.css";
161206 const mainChallenge = formData.get("form-challenge");
162207 const expectedStartDate = formData.get("form-start-date");
163208
164- const emailInput = form.querySelector<HTMLInputElement>("#form-email");
209+ const consentInput =
210+ form?.querySelector<HTMLInputElement>("#form-consent");
165211
212+ const isNameInputValid =
213+ nameInput.validity.valid && !nameInput.validity.valueMissing;
214+ const isEmailInputValid =
215+ emailInput.validity.valid && !emailInput.validity.valueMissing;
216+ const isConsentInputValid = consentInput?.validity.valid;
166217
167- if (!name || !email || !consent) {
168- notificationElem.classList.add("text-red-500");
218+ if (!isNameInputValid) {
219+ handleValidation(
220+ nameInput,
221+ notification.frontmatter.fullnameLength,
222+ form,
223+ );
224+ }
225+
226+ if (!isEmailInputValid) {
227+ handleValidation(
228+ emailInput,
229+ notification.frontmatter.emailInvalid,
230+ form,
231+ );
232+ }
169233
170- if (!name) {
171- notificationElem.textContent = notification.frontmatter.fullnameLength;
172- } else if (!email) {
173- notificationElem.textContent = notification.frontmatter.emailInvalid;
174- } else if (!consent) {
175- notificationElem.textContent = notification.frontmatter.consentRequired;
234+ if (
235+ !nameInput.validity.valid ||
236+ !emailInput.validity.valid ||
237+ !isConsentInputValid
238+ ) {
239+ if (!consent) {
240+ consentInput?.classList.replace(
241+ "border-gray-300",
242+ "border-red-600",
243+ );
244+ consentInput?.classList.add("border-2");
176245 }
177246 return;
178247 }
248+ notificationElem.textContent = "";
179249
180250 const isPageSubmitter =
181251 (e.submitter as HTMLButtonElement).value === "page";
0 commit comments