Tom
Tom

Reputation: 12998

Check if input is empty on submit

I am trying to check whether or not the password field on a form has been filled in on submission on a form but it always returns as "undefined" so the code I'm trying to run when the form is submitted never runs.

The username field seems to work fine though.

Here's my code...

$("#form1").find("input[type=submit]").click( function() {
    if($(".username").val() != "" && $(".password").val() != "") {
        console.log("Success")
    } else {
        console.log("fail");
    }
});

Upvotes: 0

Views: 986

Answers (1)

Trufa
Trufa

Reputation: 40717

Something must be wrong with your html since it seems to work:

<form id="form1" method="post">
    <input type="text" class="username">
    <input type="text" class="password">
    <input type="submit" value="Submit">
</form>

See live example here.

I have a feeling that you might be confusing classes that are selected with a "." with id's that are selected with a "#", but if I don't see your html, I'm just guessing.

Upvotes: 1

Related Questions