Bootstrap 5 Form Validation

Form Validation

You can use different validation classes to provide valuable feedback to users. Please refer to .was-validated or .needs-validation added to the <form> element, depending on whether you want to provide validation feedback before or after submitting the form. Input fields will have a green (valid) or red (invalid) border to indicate what is missing in the form. You can also add .valid-feedback or .invalid-feedback messages to clearly inform the user of what is missing or what needs to be completed before submitting the form.

Example

In this example, we use .was-validated To indicate what is missing:

<form action="/action_page.php" class="was-validated">
  <div class="mb-3 mt-3">
    <label for="uname" class="form-label">Username:</label>
    <input type="text" class="form-control" id="uname" placeholder="Please enter username" name="uname" required>
    <div class="valid-feedback">Valid.</div>
    <div class="invalid-feedback">Please fill in this field.</div>
  </div>
  <div class="mb-3">
    <label for="pwd" class="form-label">Password:</label>
    <input type="password" class="form-control" id="pwd" placeholder="Please enter password" name="pswd" required>
    <div class="valid-feedback">Valid.</div>
    <div class="invalid-feedback">Please fill in this field.</div>
  </div>
  <div class="form-check mb-3">
    <input class="form-check-input" type="checkbox" id="myCheck" name="remember" required>
    <label class="form-check-label" for="myCheck">I agree to these terms.</label>
    <div class="valid-feedback">Valid.</div>
    <div class="invalid-feedback">Check this box to continue.</div>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

Try It Yourself