Aessandro
Aessandro

Reputation: 5771

jQuery each function with if stament

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

Answers (2)

Anujith
Anujith

Reputation: 9370

Use

if(email.val() === '') {...}

or

if(!email.val()) {...}

Upvotes: 0

CD..
CD..

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

Related Questions