Form validation with custom 'onFormSubmit'

Hi!
I’m using an embed hubspot form that use the onFormSubmit function to redirect the user to a specific page based on inputs.

I cannot figure out how to prevent the form to be triggered and redirection to be fired when some input failed on validation.

→ How can I prevent redirection to happen if fields are not all valid?

Here below my current script.

<
script >
 hbspt.forms.create({
 region: "xxx",
 portalId: "xxx",
 formId: "xxx,
 inlineMessage: "Loading...",
 onFormSubmit: function($form) {

 var firstname = $form.find('input[name="firstname"]').val();
 var email = $form.find('input[name="email"]').val();

 var url = $form.find('input[name="0-2/domain"]').val();
 const regex = /^(https?:\/\/)?(www\.)?([^\/\s]+)([^\s]*)/i;
 const match = url.match(regex);
 const domain = match[3]; // example.com
 const pageUrl = window.location.hostname;
 $('input[name="0-2/page_url"]').val(window.location.hostname).change();
 setTimeout(function() {
 window.location.href = "https://" + pageUrl + "/sign-up-additional?orgid=" + encodeURIComponent(orgId2) + "&email=" + encodeURIComponent(email) + "&pageUrl=" + encodeURIComponent(pageUrl);
 }, 10); 
 }
 }); <
/script>

Hi, @mpr :waving_hand: Thanks for reaching out. Hey, @piersg @albertsg, do you have any thoughts here? I found a few related threads here and here.

Best,

Jaycee