Bootstrap 4 Advanced Components : Form validation v2
Bootstrap 4 Advanced Components : Form validation v2 with Parsley
by djibe89
HTML
<link rel="stylesheet" href="https://daemonite.github.io/material/css/material.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js"></script>
<script src="https://daemonite.github.io/material/js/material.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/parsleyjs@2.9.1/dist/parsley.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i,700,700i" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="container">
<div class="row">
<div class="col">
<div class="jumbotron my-4">
<h1>Form validation v2 for Bootstrap 4 Advanced Components</h1>
<p class="lead">by djibe.
<br>Just try to submit this form to test, based on <a href="http://parsleyjs.org/" target="_blank">Parsley validation plugin</a>.</p>
<p>
Just include Parsley js file in your project to start playing (jQuery is required).
<br> If you don't want a real-time form validation, but "on submit" validation, add .was-validated class to the <form>.<br> Just use the HTML5 code required for each input.
</p>
<p>
Google recommends not to use green border when field is ok. So it is recommended to comment the successClass in the javascript of this fiddle.
</p>
<p>
Localization files are available. See more info on Parsley doc.
</p>
<p>
TODO: Textarea and button group validation.
</p>
<form role="form" id="my-form">
<div class="form-group">
<label for="name">Text field with min and maxlength</label>
<input type="text" class="form-control" id="name" name="name" minlength="4" maxlength="32" required autocomplete="off" data-parsley-required-message="This text field is required! (custom...
CSS
/* Don't add this code except if you are using Daemonite material UI which is still BS 4.1.1 */
.was-validated .form-control:invalid, .form-control.is-invalid {
border-color: #dc3545;
padding-right: calc(1.5em + 0.75rem);
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23dc3545' viewBox='-2 -2 7 7'%3e%3cpath stroke='%23dc3545' d='M0 0l3 3m0-3L0 3'/%3e%3ccircle r='.5'/%3e%3ccircle cx='3' r='.5'/%3e%3ccircle cy='3' r='.5'/%3e%3ccircle cx='3' cy='3' r='.5'/%3e%3c/svg%3E");
background-repeat: no-repeat;
background-position: center right calc(0.375em + 0.1875rem);
background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
}
.was-validated .form-control:valid, .form-control.is-valid {
border-color: #28a745;
padding-right: calc(1.5em + 0.75rem);
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3e%3cpath fill='%2328a745' d='M2.3 6.73L.6 4.53c-.4-1.04.46-1.4 1.1-.8l1.1 1.4 3.4-3.8c.6-.63 1.6-.27 1.2.7l-4 4.6c-.43.5-.8.4-1.1.1z'/%3e%3c/svg%3e");
background-repeat: no-repeat;
background-position: center right calc(0.375em + 0.1875rem);
background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
}
JavaScript
// Parsley plugin initialization with tweaks to style Parsley for Bootstrap 4
$("#my-form").parsley({
errorClass: 'is-invalid text-danger',
successClass: 'is-valid', // Comment this option if you don't want the field to become green when valid. Recommended in Google material design to prevent too many hints for user experience. Only report when a field is wrong.
errorsWrapper: '<span class="form-text text-danger"></span>',
errorTemplate: '<span></span>',
trigger: 'change'
}) /* If you want to validate fields right after page loading, just add this here : .validate()*/ ;
// Parsley full doc is avalailable here : https://github.com/guillaumepotier/Parsley.js/