user2203362
user2203362

Reputation: 259

jQuery Form Errors

I have an simple form that's not meant to functional, it's just for a prototype. Here's the HTML:

<form class="signup">
  <div class="group">
    <div class="row">
      <label>Username</label>
      <input type="text">
    </div>
    <div class="row">
      <label>Password</label>
      <input type="password">
    </div>
  </div>

  <input type="submit" value="Sign In">
</form>

I need to have it so if a user was to input some data (dummy data) into the username and password and click submit, it'll show a JavaScript alert saying "Logged in" and if one of the inputs doesn't contain any data, I'd like it to show a JavaScript alert saying "Error".

I want to use jQuery and this is what I have so far:

$('.signup').submit(function () {
  if($(this).valid()) {
    alert('Logged in');
  }else {
    alert('Error!');
  }
});

Any reason why that's not working?

Upvotes: 0

Views: 114

Answers (2)

tenub
tenub

Reputation: 3446

The following should work for your latest question:

$('.signup').submit(function () {
    if( $('input[type="text"]').val()!='' && $('input[type="password"]').val()!='' ) {
        alert('Logged in');
    }
    else {
        alert('Error!');
    }
});

Upvotes: 0

Dave Kiss
Dave Kiss

Reputation: 10485

It looks like you're attempting to use the jQuery validation plugin. Make sure you're including that on your page as well, as valid() is not a function that is included with jQuery.

Upvotes: 2

Related Questions