Skip to content

Instantly share code, notes, and snippets.

@anupammaiti
Created October 23, 2024 22:57
Show Gist options
  • Select an option

  • Save anupammaiti/a1a8b32eb8ebf44859f6c3de4fc24ed9 to your computer and use it in GitHub Desktop.

Select an option

Save anupammaiti/a1a8b32eb8ebf44859f6c3de4fc24ed9 to your computer and use it in GitHub Desktop.
test
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