AnApprentice
AnApprentice

Reputation: 110950

JavaScript IF with AND/OR.. not working

Can someone who is a master at JS tell me what's wrong with this?

if ( $.trim($("#add-box-text").val()).length < 2 && $.trim($("#add-box-text").val()) != "Click here to add an item" ) {
    // If it's LT than 1 Character, don't submit
    $("#add-box-text").effect('highlight', {color: '#BDC1C7'}, 500);

    // Refocus
    $("#add-box-text").focus();
}

Upvotes: 0

Views: 69

Answers (1)

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

For one thing, if it's less than 2 characters, it's never going to equal that string.

EDIT: Modified to reflect your comments. You want to check that it's >= and not equal to that string.

var trimmed = $.trim($("#add-box-text").val());
if ( trimmed.length >= 2 && trimmed != "Click here to add an item" ) {
    $("#add-box-text").effect('highlight', {color: '#BDC1C7'}, 500);

    // Refocus
    $("#add-box-text").focus();
}

Upvotes: 1

Related Questions