APIs & Integrations

KVyskočilová
メンバー

Custom validation or prevent form submission

解決

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

0 いいね!
1件の承認済みベストアンサー
piersg
解決策
キーアドバイザー

Custom validation or prevent form submission

解決

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:

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. 

元の投稿で解決策を見る

6件の返信
CVlad
メンバー

Custom validation or prevent form submission

解決

@KVyskočilová Try this solution I added here.

Let me know if it works for you.

0 いいね!
piersg
キーアドバイザー

Custom validation or prevent form submission

解決

Hi @KVyskočilová (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.

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

 

0 いいね!
KVyskočilová
メンバー

Custom validation or prevent form submission

解決

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

0 いいね!
dennisedson
HubSpot製品開発チーム
HubSpot製品開発チーム

Custom validation or prevent form submission

解決

@KVyskočilová 

This thread has a possible solution for you.    @piersg 👋, any thoughts?

0 いいね!
KVyskočilová
メンバー

Custom validation or prevent form submission

解決

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.

0 いいね!
piersg
解決策
キーアドバイザー

Custom validation or prevent form submission

解決

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:

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.