question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation is missing how to reset the form validation

See original GitHub issue

I haven’t found a solution… What annoys me the most is that the documentation does not show how to reset or disable validation. I shouldn’t have to search the web for this common use case.

I want to clear the form after submission but the validation immediately jumps in and validates the now empty form fields again, resulting in showing errors.

	function formValidation() {
		// Validation -> https://getbootstrap.com/docs/4.0/components/forms/#validation
		// Fetch all the forms we want to apply custom Bootstrap validation styles to
		var forms = document.getElementsByClassName('needs-validation');
		// Loop over them and prevent submission
		var validation = Array.prototype.filter.call(forms, function (form) {
			form.addEventListener('submit', function (event) {
				if (form.checkValidity() === false) {
					event.preventDefault();
					event.stopPropagation();
					$(form).addClass('is-invalid');
				} else {
					$(form).addClass('is-valid');
				}
				form.classList.add('was-validated');
			}, false);
		});
	}

	function contactForm() {
		// Honeypot
		let honeypot = $('#contact-website');
		let contactForm = $('#contact-form');

		honeypot.parent('.form-group').hide();
		honeypot.removeAttr('required');

		contactForm.on('submit', (event) => {
			event.preventDefault();

			if (!contactForm.hasClass('is-valid')) {
				return;
			}

			$.ajax({
				type: 'POST',
				url: 'contact.php',
				data: contactForm.serialize(),
				success: () => {
					$('#contact-form input').val('');
					$('#contact-form textarea').val('');
					$('#modal').modal('show');
				}
			});
		});
	}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
patrickhlaukecommented, Feb 14, 2018

if you form wasn’t doing custom AJAX stuff and not actually submitting itself, the code provided in the documentation would work just fine. once you go off-piste with non-standard behaviour, then yes things will get bumpy. but we can’t include (nor foresee) all those situations in the documentation.

anyway, even though this is not a general case, but rather something that is down to your own implementation…here’s a rough solution https://codepen.io/patrickhlauke/pen/wyreOg

1reaction
kulketancommented, Apr 28, 2021

I’m using below code to reset the form after successful validation and not get the red border.

var forms = document.getElementsByClassName('needs-validation');
		// Loop over them and prevent submission
		var validation = Array.prototype.filter.call(forms, function (form) {
			form.addEventListener('submit', function (event) {
				if (form.checkValidity() === false) {
					event.preventDefault();
					event.stopPropagation();
				} 
				form.classList.add('was-validated');
                                if(form.checkValidity() === true){
                                        //for is validated successfully. Here you can write ajax call or whatever
                                        form.classList.remove("was-validated");
                                        form.reset();
                                }
			}, false);
		});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to reset form validation on submission of the form in ...
To make the form empty call this.contactForm.reset(); and the form will be invalid , it's fine. However, you also need to reset the...
Read more >
Reactive Form - Reset input validation after change
I know I can set each of the form's input value for .valid and .message to clear these warnings - say - on...
Read more >
Handling Forms - VeeValidate
vee-validate offers many helpers to handle form submissions, resets, and DX to make your forms much easier to reason about and less of...
Read more >
Bootstrap Validation - examples & tutorial
HTML form validation is applied via CSS's two pseudo-classes, :invalid and :valid . · MDB scopes the :invalid and :valid styles to parent...
Read more >
Validation · Bootstrap v5.0
To reset the appearance of the form (for instance, in the case of dynamic form submissions using AJAX), remove the .was-validated class from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found