Reputation: 5771
I am building a function to validate forms, which I can use for upcoming projects. The second if stament is not working, I can't figure out why.
$('.submit').click(function() {
var hasError = false,
emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/,
form = $('form'),
radio_check = $('.tick .test'),
input = $('input:not(.submit)'),
emailaddressVal = $("#email").val(),
email = $('#email');
$('form input:not(.submit)').each(function(){
if ($(this).val() == '') {
$(this).css('background-color', 'red');
if(!email.val() == '') {
email.css('background-color', '#faa');
}
}
});
});
Upvotes: 0
Views: 63
Reputation: 74176
I suppose your meaning is to check that the email field is not empty, so you should do:
if(!email.val()) { } //-> not falsey
Or
if(email.val() === '') { } //-> is empty
Upvotes: 1