Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap5 Form Validation

Bootstrap5 Form Validation

We can use different validation classes to set up form validation.

Add .was-validated or .needs-validation to the

element, and the input fields will have green (valid) or red (invalid) border effects to indicate whether the form requires input.

The .valid-feedback or .invalid-feedback classes tell users what information is missing or what needs to be completed before submitting the form.

Example

Use the .was-validated class to show what needs to be filled in before the form is submitted:

<form action="" class="was-validated">
<div class="form-group">
<label for="uname">

Username:

label

<input type="text" class="form-control" id="uname" placeholder=">

Enter username

"

name

=

"

uname

"

required

<div class="valid-feedback">

Validation successful!

div

<div class="invalid-feedback">

Please enter your username!

div

div

<div class="form-group">
<label for="pwd">

Password:

label

<input type="password" class="form-control" id="pwd" placeholder=">

Enter password

"

name

=

"

pswd

"

required

<div class="valid-feedback">

Validation successful!

div

<div class="invalid-feedback">

Please enter your password!

div

div

<div class=">

form-group form-check

"

<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="remember" required>

I agree to the terms

<div class="valid-feedback">

Validation successful!

div

<div class="invalid-feedback">

You must agree to the terms before submitting.

div

label

div

<button type="submit" class=">

btn btn-primary

"

Submit

button

form

Try it »

Example

Use .needs-validation, which validates missing content after the form is submitted. Some JavaScript code needs to be added here to make it work:

<form action="" class="needs-validation" novalidate>
<div class="form-group">
<label for="uname">

Username:

label

<input type="text" class="form-control" id="uname" placeholder=">

Enter username

"

name

=

"

uname

"

required

<div class="valid-feedback">

Validation successful!

div

<div class="invalid-feedback">

Please enter your username!

div

div

<div class="form-group">
<label for="pwd">

Password:

label

<input type="password" class="form-control" id="pwd" placeholder=">

Enter password

"

name

=

"

pswd

"

required

<div class="valid-feedback">

Validation successful!

div

<div class="invalid-feedback">

Please enter your password!

div

div

<div class=">

form-group form-check

"

<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="remember" required>

I agree to the terms

<div class="valid-feedback">

Validation successful!

div

<div class="invalid-feedback">

You must agree to the terms before submitting.

div

label

div

<button type="submit" class=">

btn btn-primary

"

Submit

button

form

<

script

// If validation fails, prevent form submission (function() { 'use strict'; window.addEventListener('load', function() { // Get form validation styles var forms = document.getElementsByClassName('needs-validation'); // Loop 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'); }, false); }); }, false); })();

script

Try it »