user3495224
user3495224

Reputation: 49

Statement "else if" dont work to validate input

Just got to do the "if" statement, but I don't understand why the "else if" does not work.

Jquery

$('#submit-register').click(function () {
    // Works Fine
    if (0 == $('#last').val().length) {
        $('#last').addClass('error-input');
      // Doesn't Work
    } else if (0 == $('#name').val().length) {
        $('#name').addClass('error-input');
    }
});

Upvotes: 0

Views: 62

Answers (2)

ShujatAli
ShujatAli

Reputation: 1376

your might had to open console from check the values for testing

$('#submit-register').click(function () {
    // Work Fine
    console.log("first if value = "+$('#last').val().length);
    console.log("second if value = "+ $('#name').val().length);
    if (0 == $('#last').val().length) {
      console.log("first if is true");
        $('#last').addClass('error-input');
      // Dont Wok
    } else if (0 == $('#name').val().length) {
      console.log("second if is true");
        $('#name').addClass('error-input');
    }
else {
      console.log("no if is true");
}

});

Upvotes: 0

John Boker
John Boker

Reputation: 83709

You probably want to validate each condition separately.

to do that you need to remove the else.

$('#submit-register').click(function () {
    // Works Fine
    if (0 == $('#last').val().length) {
        $('#last').addClass('error-input');
    } 

    if (0 == $('#name').val().length) {
        $('#name').addClass('error-input');
    }
});

Upvotes: 3

Related Questions