Rich-text Reply

Form Validation

fsoria 01-26-15
Accepted Solution

Form Validation

[ Edited ]

I have setup an experiment where I added a user agreement (UA) checkbox.  As part of the requirements, the form should not be submitted if the user has not checked the UA checkbox.  I've added the following script to my experiment:

 

$('form.lead-form').bind('submit', function() {
  if (!($('#user-terms-agree').attr('checked'))) {
    $('.user-agree-terms').addClass('error').append('<div class="error-text" id="uta">You must agree to the terms and conditions.</div>');
    return false;
  }
});

 

In preview, the form still submits even though the checkbox is not checked.  What am I missing?

 

I've also tried preventDefault() and same result.

Level 2

fsoria 01-26-15
 

Re: Form Validation

 

Solved! Added stopImmediatePropagation() to script;

 

$('form.lead-form').bind('submit', function(e) {
  if (!($('#user-terms-agree').attr('checked'))) {
    $('.user-agree-terms').addClass('error').append('<div class="error-text" id="uta">You must agree to the Terms of Use and Privacy Policy.</div>');
    e.stopImmediatePropagation();
    return false;
  }
});

 

Level 2
Amanda 01-27-15
 

Re: Form Validation

This is great! Thanks for sharing the solution you found Smiley Happy

Optimizely