How to Run Custom Validation Before a Form Submits

Follow

If you need to run custom validation before a Classic Builder form submits, such as an asynchronous check, you can pause the form's submit action, run your check, and then submit the form yourself.

This is community code, not an official Unbounce feature. It changes the form's built-in submit behavior, so it can break form submission if it's set up incorrectly, and Unbounce form internals can change over time. Unbounce Support can't troubleshoot custom code. Test it thoroughly on a published page.

Pause and resume the submit

This script removes the submit button's click handler, runs your own logic, and then submits the form. Replace the comment with your validation:

<script>
lp.jQuery().ready(function () {
  var id = window.module.lp.form.data.formButtonId;
  lp.jQuery("#" + id).unbind("click tap touchstart");
  lp.jQuery("#" + id).bind("click tap touchstart", function (e) {
    e.preventDefault();
    e.stopPropagation();
    // Your custom validation here
    lp.jQuery("form").submit();
  });
});
</script>

The script handles clicks and taps on the submit button. To also handle the Enter key, you need to rebind the form's keypress event. For a general approach to running code when a form submits, see How to Run Custom Scripts on Form Submission.