Tim Aych
Tim Aych

Reputation: 1365

jquery validator - form still getting submitted even when invalid

I have a few fields on a form with basic rules and the validator plugin does well when filling out the form itself. Submitting it, however, there is a problem because it still submits even when a valid email address hasn't been put in or before anything has been entered in other text boxes.

Here's my test function:

$(function() {

    $("#submit_form_contact").validate({
        errorLabelContainer: '#errors'
    });

    $('#contact_submit').click(function(e){

        if ($("#submit_form_contact").valid()) {
            alert ('valid!');
        } else {
            alert ('invalid!');
        }
    });
});

HTML:

<form class="pure-form pure-form-aligned" id="submit_form_contact" novalidate>
  <fieldset>
    <div class="pure-control-group">
      <label for="name">Your Name:</label>
      <input id="name" type="text" placeholder="Name" required>
    </div>

    <div class="pure-control-group">
      <label for="email">Your Email:</label>
      <input id="email" type="email" placeholder="Email Address" required>
    </div>

    <div class="pure-control-group">
      <label for="email_text">Inquiry Type: </label>
      <select id="inquiry_dropdown" class="pure-input-1-2">
        <option>General</option>
        <option>Sales & Marketing</option>
        <option>Press & Editorial</option>
      </select>
    </div>

    <div class="pure-control-group">
      <label for="message" style="vertical-align: top;">Message:</label>
      <textarea id="message" type="text" placeholder="Enter message here..." required></textarea>
    </div>                      

    <div id="errors" style="text-align: center; color: red;"></div>

    <button id="contact_submit" class="pure-button pure-button-primary" style="background-color: #003A70; float:right; margin-right: 35px;margin-top:15px;">Submit</button>
</div>
</fieldset>
</form>

...It still alerts as valid every time. All fields in the form are marked 'required'. Any help would be much appreciated. JSFiddle: http://jsfiddle.net/RcNTj/

Upvotes: 1

Views: 3487

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Change the id attribute to name and try

<input name="name" type="text" placeholder="Name" required>

Demo: Fiddle

Upvotes: 4

Related Questions