I'm trying to create an email validation for my form but i'm lost.
JS:
$(function () {
'use strict';
// error variables
var UserError = true,
EmailError = true,
SubError = true,
MsgError = true,
emailReg = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/,
email = $(".email").val();
$(".email").blur(function() {
if($(this).val() === ''){
$(this).css('border','1px solid #dc2817');
$(this).parent().find('.custom-alert').fadeIn(300).end().find('.asterix').fadeOut(300).end().find('span.cross').fadeIn(300).end().find('span.verify').fadeOut(300);
EmailError = true;
alert("1.");
}
else if(emailReg.test(email)){
$(this).css('border','1px solid #080');
$(this).parent().find('.custom-alert').fadeOut(300).end().find('.asterix').fadeOut(300).end().find('span.verify').fadeIn(300).end().find('span.cross').fadeOut(300);
EmailError = false;
alert("2");
}
else{
$(this).css('border','1px solid #dc2817');
$(this).parent().find('.custom-alert').fadeIn(300).end().find('.asterix').fadeOut(300).end().find('span.cross').fadeIn(300).end().find('span.verify').fadeOut(300);
EmailError = true;
alert("3");
}
});
$('.contact-form').submit(function(event) {
if(EmailError === true){
event.preventDefault(); // prevent sending
$('.email').blur();
}
});
With this i only get alert 1 (without value) or alert 3 (with value). If i try to type an e-mail, still get alert 3, when this should give me alert 2.
If i try to !emailReg.test(email), i get alert 2.
Please enlighten me!
Thank you in advance.
BR