How do you validate a form with Bootstrap 5?

const form = document.querySelector('.needs-validation');
form.addEventListener('submit', event => {
  if (!form.checkValidity()) {
    event.preventDefault();
    event.stopPropagation();
  }
  form.classList.add('was-validated');
});

Write .valid-feedback and .invalid-feedback messages under the fields. For server side results, add .is-valid and .is-invalid on the fields. Always validate again on the server. See Form Validation.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.