$('.infusion-form').on('submit', function() {
const emailFields = $('input[type="email"]', this);
let error = 0,
firstErrorElement = 0;
emailFields.each(function() {
if(!validateEmail($(this).val())) {
$(this).addClass('error-input');
error = 1;
firstErrorElement === 0 ? firstErrorElement = $(this) : '';
} else {
$(this).removeClass('error-input');
}
})
if(error === 1) {
$('html,body').animate({
scrollTop: firstErrorElement.offset().top - 85
}, 800);
return false;
}
return false;
})
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}