# 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:** 1\
**Showing post:** 4

<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.

---

_[View the full topic](https://community.hubspot.com/t/custom-validation-or-prevent-form-submission/61144)._
