Bootstrap plugin

Integrate with the Bootstrap framework
The plugin supports Bootstrap v4.3.1.
Use the Bootstrap5 plugin if your form is made in Bootstrap v5

Usage

The following piece of code is the starting point to validate the form made in Bootsrap 4:
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" />
<link rel="stylesheet" href="/vendors/@form-validation/umd/styles/index.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/@form-validation/umd/bundle/popular.min.js"></script>
<script src="/vendors/@form-validation/umd/plugin-bootstrap/index.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function(e) {
FormValidation.formValidation(
document.getElementById('demoForm'),
{
fields: {
...
},
plugins: {
bootstrap: new FormValidation.plugins.Bootstrap(),
...
},
}
);
});
</script>
</body>
</html>
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.
The next sections list out some examples of various forms made with Bootstrap 4.

Using the npm packages

If you are using a bundler such as Webpack, Rollup, Parcel or Vite, etc., to bundle your application, then it's recommended to use the FormValidation NPM packages.
  • Install the packages:
$ npm install @form-validation/bundle
$ npm install @form-validation/plugin-bootstrap
  • Import and use the Bootstrap plugin:
import { formValidation } from '@form-validation/bundle/popular';
import { Bootstrap } from '@form-validation/plugin-bootstrap';
formValidation(
document.getElementById('demoForm'),
{
fields: {
...
},
plugins: {
bootstrap: new Bootstrap({
...
}),
...
},
}
);

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 .form-group 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:
bootstrap: new FormValidation.plugins.Bootstrap({
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 '.form-group';
}
}
}),
Multiple fields on the same row

Changelog

v2.4.0
v2.0.0
  • Add the npm package
v1.5.0
  • Fixed the duplicated icons when using the Icon plugin with Bootstrap 4.3
v1.2.0
  • Support Bootstrap v4.1.2
v1.0.0
  • First release