Bootstrap5 plugin

Integrate with Bootstrap 5 framework
The plugin supports Bootstrap v5.1.1.
Use the Bootstrap, Bootstrap3 plugins if you are still using Bootstrap v4, v3

Usage

The following piece of code is the starting point to validate the form made in Bootsrap 5:
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="/vendors/formvalidation/dist/css/formValidation.min.css" />
</head>
<body>
<form id="demoForm" method="POST">...</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.min.js"></script>
<script src="/vendors/formvalidation/dist/js/FormValidation.min.js"></script>
<script src="/vendors/formvalidation/dist/js/plugins/Bootstrap5.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function(e) {
FormValidation.formValidation(
document.getElementById('demoForm'),
{
fields: {
...
},
plugins: {
bootstrap: new FormValidation.plugins.Bootstrap5(),
...
},
}
);
});
</script>
</body>
</html>
There are some important things here:
  • The sample code above assumes that the FormValidation files are placed inside the vendors directory. You might need to change the path depending on where you place them on the server.
  • Bootstrap5.min.js is the plugin provided by FormValidation. It is NOT the same as Bootstrap(.min).js file provided by Bootstrap framework.
The next sections list out some examples of various forms made with Bootstrap 5.

Horizontal form

Horizontal form

Stacked form

Stacked form

Inline form

Inline form

Form without label

Form without label

Multiple fields on the same row

In order to add the correct class for error message and the field element when it is a valid or invalid, we need to specify the CSS selector of the field container.
By default, the Bootstrap plugin will look for the .row element. In the following example, the firstName and lastName fields are placed inside .col-sm-4 containers. Meanwhile, the city, state and zipcode fields can be found inside the .col-sm-3 containers.
The rowSelector option will be used to help the plugin determine the field containers as following:
bootstrap5: new FormValidation.plugins.Bootstrap5({
rowSelector: function(field, ele) {
// field is the field name
// ele is the field element
switch (field) {
case 'firstName':
case 'lastName':
return '.col-sm-4';
case 'city':
case 'state':
case 'zipcode':
return '.col-sm-3';
default:
return '.row';
}
}
}),
Multiple fields on the same row

Changelog

v1.9.0
  • The Boostrap 5 select element has duplicate icons
v1.8.1
  • Support floating labels
  • Fix an issue that the border radius of input groups is lost
  • Fix an issue that it shows error message at a wrong place when using with add-on input
v1.8.0
  • First release