I created a form starting from the example in the docs in "Validation - Custom styles" section. I added "required" attr to the cumpolsory inputs and validation happens well. Now I would need to add custom valdation to a field that must respect a particular format.
I created the function validaForm() to validate the field and to add the "invalid" class input to it. I would need help in how to use the function in the code *** provided by the Bootstrap example:
// My custom function to validate the field
function validaForm() {
var $inputs = $("#frmIscrCant :input:not([readonly])");
//var values = {};
$inputs.each(function () {
//values[this.id] = $(this).val();
if ($(this).hasClass("partIVAInput") && $(this).val().trim() != "")
if (!isValidPIVA($(this).val())) {
$(this).addClass("is-invalid");
alert("P.IVA non valida!");
}
});
}
// Code provided by Bootstrap example(***). In this function I need to insert the function above
(function() {
'use strict';
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
validaForm(); // !!!
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
</script>