Bootstrap5 form validation
We can use different validation classes to set up the validation function of the form.
.was-validated
or .needs-validation
add to <form>
element, the input
input field will have a green (valid) or red (invalid) border effect to indicate whether the form requires input.
.valid-feedback
or .invalid-feedback
class is used to tell the user what information is missing or what needs to be done before submitting the form.
Example
Use .was-validated
class displays what the form needs to fill in beforeit 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">Verification 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">Verification successful!</div>
<div class="invalid-feedback">Please enter password!</div>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="remember" required> consent agreement
<div class="valid-feedback">Verification successful!</div>
<div class="invalid-feedback">Agree to the agreement before submitting.</div>
</label>
</div>
<button type="submit" class="btn btn-primary">submit to</button>
</form>
Example
Use .needs-validation
which validates the missing content after the form is submitted You need to add some JavaScript code here to make the codework:
<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">Verification successful!</div>
<div class="invalid-feedback">enter one user name!</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">Verification successful!</div>
<div class="invalid-feedback">Please enter password!</div>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="remember" required> consent agreement
<div class="valid-feedback">Verification successful!</div>
<div class="invalid-feedback">Agree to the agreement before submitting。</div>
</label>
</div>
<button type="submit" class="btn btn-primary">submit to</button>
</form>
<script>
// If the verification fails, prohibit submitting the form
(function() {
'use strict';
window.addEventListener('load', function() {
// Get form validation style
var forms = document.getElementsByClassName('needs-validation');
// Loop and prohibit 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>