jqueryjquery-validation-engineajax-forms

Jquery Validation Engine : Form submitting Without Validation


i am using Jquery Validation Engine (https://github.com/posabsolute/jQuery-Validation-Engine) for one of my forms. The Validation works well but is not able to stop the form from getting submitted even when the field values are not complying with the validation:

I have a Newsletter Sigunp form as follows:

<form id="submit-newsletter" method="POST" action="">
   <input type="text" class="searchform validate[required, custom[email]]" value="Enter Your Email" id="email" name="nw-email" onblur="defaultInput(this);" onfocu    s="clearInput(this);" />
   <input type="hidden" id="newsletter" name="newsletter" value="nl" />
   <input type="submit" class="submit" title="Signup" value="Sign Up" name="newsletter-signup" onclick="newsletterSubscribe(event);" />
</form>

This form is submitted through ajax as follows:

// Newsletter Subscription Without Refresh

function newsletterSubscribe(e) {
  e.preventDefault();
  var dataString = 'nw-email=' + $('input[name=nw-email]').val() + '&newsletter=' + $('input[name=newsletter]').val();
  $.ajax({
    type: "POST",
    url: "/newsletter/",
    data: dataString,
    success: function () {
      $('#newsletter-box').html('<div id="message"></div>');
      $('#message').html('<img width="18px" height="18px" src="/static/img/smtick.png" /><h5>Thank You !</h5>')
      .append('<p>We have recieved your request. Please Check your mail for activation instructions. You will start recieving our newsletter once you have verified t    his email address.</p>')
      .hide()
      .fadeIn(1200);
      }
  });
  return false;
}

The validation Trigger is as follows:

$(document).ready(function() {
  $('#submit-newsletter').validationEngine();
});

Now i can see the validation error message but if i press submit button, form is submitted regardless of the fact that value is not an email address.

Any Ideas ?


Solution

  • move the code into a click event handler and bind it using jquery. Then test that your form is valid using the validation engine validate method.

    $(function(){
        $('#newsletter-signup').click(function(e){
             e.preventDefault();
    
             //if invalid do nothing
             if(!$("#submit-newsletter").validationEngine('validate')){
             return false;
              }
    
    
          var dataString = 'nw-email=' + $('input[name=nw-email]').val() + '&newsletter=' + $('input[name=newsletter]').val();
          $.ajax({
            type: "POST",
            url: "/newsletter/",
            data: dataString,
            success: function () {
              $('#newsletter-box').html('<div id="message"></div>');
              $('#message').html('<img width="18px" height="18px" src="/static/img/smtick.png" /><h5>Thank You !</h5>')
              .append('<p>We have recieved your request. Please Check your mail for activation instructions. You will start recieving our newsletter once you have verified t    his email address.</p>')
              .hide()
              .fadeIn(1200);
              }
          });
          return false;
        })
    });