sonterix
1/11/2019 - 12:43 PM

Email validation

    $('.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);
    }
.error-input {
  border: 1px solid #c51244 !important;
  background: #fff0f4 !important;
  color: #c51244 !important;
}