xxxxxxxxxx
1
<form class="needs-validation" novalidate>
2
<select
3
data-mdb-select-init
4
id="select-with-validation"
5
data-mdb-validation="true"
6
data-mdb-valid-feedback="This value is valid"
7
data-mdb-invalid-feedback="This value is invalid"
8
data-mdb-clear-button="true"
9
multiple required
10
>
11
<option value="1">One</option>
12
<option value="2">Two</option>
13
<option value="3">Three</option>
14
<option value="4">Four</option>
15
<option value="5">Five</option>
16
<option value="6">Six</option>
17
<option value="7">Seven</option>
18
<option value="8">Eight</option>
19
</select>
20
<button type="submit" id="submit" class="btn btn-primary btn-sm mt-3" data-mdb-ripple-init>
21
Submit
22
</button>
23
</form>
1
1
xxxxxxxxxx
1
(() => {
2
'use strict';
3
4
// Fetch all the forms we want to apply custom Bootstrap validation styles to
5
const forms = document.querySelectorAll('.needs-validation');
6
7
// Loop over them and prevent submission
8
Array.prototype.slice.call(forms).forEach((form) => {
9
form.addEventListener('submit', (event) => {
10
event.preventDefault();
11
event.stopPropagation();
12
13
form.classList.add('was-validated');
14
},false);
15
});
16
})();
Console errors: 0