Created
October 23, 2024 22:57
-
-
Save anupammaiti/a1a8b32eb8ebf44859f6c3de4fc24ed9 to your computer and use it in GitHub Desktop.
Revisions
-
anupammaiti created this gist
Oct 23, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ form.addEventListener('submit', function(event) { event.preventDefault(); let hasErrors = false; // Loop through each form element [...form.elements].forEach(input => { if (!input.checkValidity()) { console.log(`Error in field: ${input.name}`); hasErrors = true; // Optionally, show custom error messages input.setCustomValidity('This field is invalid!'); } else { input.setCustomValidity(''); // Clear any custom error messages } }); if (hasErrors) { form.reportValidity(); // Show validation errors in the UI console.log('Form has errors.'); } else { console.log('Form is valid. Proceeding with submission.'); // form.submit(); // You can proceed with form submission here } });