I want to stop form submission when codeMatched is not matched in condition. is there a way when clicking on submit button form not going to thank you a message.
{% require_js position=“footer” %}
<script>
window.addEventListener(‘message’, event => {
var promoCodes = {{new_list|tojson}};
if(event.data.type === ‘hsFormCallback’ && event.data.eventName === ‘onFormSubmit’) {
event.preventDefault();
var codeMatched = false;
for (item in event.data.data) {
if (event.data.data[item][“name”] == “affiliate_code”) {
var matchedValue = event.data.data[item][“value”];
var dynamicUrl = “{{ module.payment_link.properties.checkoutUrl }}”;
for (code in promoCodes){
if (promoCodes[code][“code”] == matchedValue) {
dynamicUrl = promoCodes[code][“url”];
window.location = dynamicUrl;
codeMatched = true;
break;
}
if (!codeMatched) {
$(“input[name=‘affiliate_code’]”).after(“<span class=‘error-message’>We are sorry, that promo code was not found</span>”);
event.preventDefault();
}
break;
}
}
}
}
});
</script>
{% end_require_js %}
Per the documentation it’s not possible to prevent a form submission using the onFormSubmit callback.
Take a look at the answer in this thread for something similar to what you’re looking for.
Instead of using an onFormSubmit listener, they use an onFormReady listener and add a click listener to the submit button. I guess you’d test if the code matched on button click and preventDefault if not.
Barry Grennan
