I need to call a tracking code when a form is submit and after it is validated using Google Tag Manager. I do not have direct access to the site code. I can only execute script from Google Tag Manager.
I've searched the web for an answer, but I could not find something that worked.
I've tried:
jQuery('#formId').on('submit',function(){
console.log('test');
})
But this prints "test" before the form validates the fields, so if I leave a field blank, it still prints "test".
I've tried different approaches that I found:
jQuery('#formId').on('submit',function(){
if (jQuery('#formId').valid()){
console.log('test');
}
})
or
jQuery('#formId').on('submit',function(){
if (jQuery(this).valid()){
console.log('test');
}
})
or
jQuery('#formId').on('submit',function(event){
if (jQuery(event).valid()){
console.log('test');
}
})
But they all return VM914:2 Uncaught TypeError: jQuery(...).valid is not a function
How can I run a script from Google Tag Manager that runs on formsubmit after the form validation?