Created
October 23, 2024 22:57
-
-
Save anupammaiti/a1a8b32eb8ebf44859f6c3de4fc24ed9 to your computer and use it in GitHub Desktop.
test
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 characters
| 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 | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment