# Custom validation or prevent form submission

**URL:** <https://community.hubspot.com/t/custom-validation-or-prevent-form-submission/61144>\
**Category:** APIs & Integrations\
**Created:** [July 18, 2022, 1:33pm UTC](https://community.hubspot.com/t/custom-validation-or-prevent-form-submission/61144 "2022-07-18T13:33:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KVyskocilova](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kvyskocilova/32/195797_2.png) [@KVyskocilova](https://community.hubspot.com/u/KVyskocilova)\
**Post date:** [July 18, 2022, 1:33pm UTC](https://community.hubspot.com/t/custom-validation-or-prevent-form-submission/61144/1 "2022-07-18T13:33:01Z")

</div>

Hi,

is there a way to hook into the validation process (ideally) or prevent form submission? We will check if the e-mail is valid against BriteVerify, but we still can’t figure out, how to achieve the blocked form submit or extend the validation.

We embed forms via HTML to our site.

Haven’t found it in the docs, but I guess there should have been a correct answer to this.

Thanks in advance!

Karolina

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [July 19, 2022, 4:54pm UTC](https://community.hubspot.com/t/custom-validation-or-prevent-form-submission/61144/2 "2022-07-19T16:54:19Z")

</div>

[@KVyskočilová](https://community.hubspot.com/t5/user/viewprofilepage/user-id/419496)

[This thread](https://community.hubspot.com/t5/APIs-Integrations/How-to-work-with-jquery-custom-validation-for-hubspot-form/m-p/226634/highlight/true#M5530) has a possible solution for you. @piersg 👋, any thoughts?

---

<div class="post-metadata">

**Author:** ![KVyskocilova](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kvyskocilova/32/195797_2.png) [@KVyskocilova](https://community.hubspot.com/u/KVyskocilova)\
**Post date:** [July 22, 2022, 3:40pm UTC](https://community.hubspot.com/t/custom-validation-or-prevent-form-submission/61144/3 "2022-07-22T15:40:58Z")

</div>

I’ve found this, but as you say - it’s not easy and developer friendly. We hooked somehow inside, but sometimes we get doubled validations since we can’t suppress yours. If you could pull out API for validation or submission, it would be more than awesome. Based on how many threads are open with the same issue, I’m not alone in this.

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [July 25, 2022, 10:13am UTC](https://community.hubspot.com/t/custom-validation-or-prevent-form-submission/61144/4 "2022-07-25T10:13:11Z")

</div>

Hmm yes, you’re right. In that case, I would attach a click event to the button, prevent the form from submitting, do validation and then submit the form if that passes:

```lia-code-sample
const btn = document.querySelector('.hs-button.primary.large');
const form = document.querySelector('[selectors for your form]');

function validation(){
 // your validation test, probably something that returns a boolean
 return true
}

btn.addEventListener('click', e => {
 e.preventDefault();
 if (validation()) {
 console.log('validation passed');
 form.submit();
 } else {
 console.log('validation failed');
 return false;
 }
});

```

I tested this out and it will prevent form submission.

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [July 21, 2022, 9:01am UTC](https://community.hubspot.com/t/custom-validation-or-prevent-form-submission/61144/5 "2022-07-21T09:01:22Z")

</div>

Hi [@KVyskočilová](https://community.hubspot.com/t5/user/viewprofilepage/user-id/419496) (thanks @dennisedson).

Yeah, there are global Hubspot form events you can tap into. onFormSubmit is your best bet:

it’s called at the start of form submission but before the submission has been persisted/sent, so you could run your checks then cancel submission if they’re not passed.

```lia-code-sample
window.addEventListener('message', event => {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
 // do things
 }
});

```

---

<div class="post-metadata">

**Author:** ![KVyskocilova](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kvyskocilova/32/195797_2.png) [@KVyskocilova](https://community.hubspot.com/u/KVyskocilova)\
**Post date:** [July 22, 2022, 3:39pm UTC](https://community.hubspot.com/t/custom-validation-or-prevent-form-submission/61144/6 "2022-07-22T15:39:20Z")

</div>

Thanks, I know about it, but they won’t let you extend Hubspot’s validation or stop the sending.

---

<div class="post-metadata">

**Author:** ![CVlad](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cvlad/32/105005_2.png) [@CVlad](https://community.hubspot.com/u/CVlad)\
**Post date:** [November 22, 2023, 12:01pm UTC](https://community.hubspot.com/t/custom-validation-or-prevent-form-submission/61144/7 "2023-11-22T12:01:19Z")

</div>

[@KVyskočilová](https://community.hubspot.com/t5/user/viewprofilepage/user-id/419496) Try this solution I added [here](https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/td-p/882793).  
Let me know if it works for you.
