# Form validation with custom 'onFormSubmit'

**URL:** <https://community.hubspot.com/t/form-validation-with-custom-onformsubmit/72821>\
**Category:** APIs & Integrations\
**Created:** [January 12, 2023, 10:41pm UTC](https://community.hubspot.com/t/form-validation-with-custom-onformsubmit/72821 "2023-01-12T22:41:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpr](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@mpr](https://community.hubspot.com/u/mpr)\
**Post date:** [January 12, 2023, 10:41pm UTC](https://community.hubspot.com/t/form-validation-with-custom-onformsubmit/72821/1 "2023-01-12T22:41:26Z")

</div>

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.

```lia-code-sample
<
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>

```

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [January 13, 2023, 6:47pm UTC](https://community.hubspot.com/t/form-validation-with-custom-onformsubmit/72821/2 "2023-01-13T18:47:05Z")

</div>

Hi, @mpr 👋 Thanks for reaching out. Hey, @piersg @albertsg, do you have any thoughts here? I found a few related threads [here](https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-or-prevent-form-submission/m-p/670190/highlight/true#M55130) and [here](https://community.hubspot.com/t5/APIs-Integrations/How-to-work-with-jquery-custom-validation-for-hubspot-form/m-p/452724/highlight/true#M44635).

Best,

Jaycee
