Hi,
MktoForms2.whenReady(function (mktoForm) {
mktoForm.onValidate(extendedEmailValidation);//Validate form
//Extended Validation
async function extendedEmailValidation(nativeValid) {
if (!nativeValid) return;
mktoForm.submittable(true);
var currentValues = mktoForm.getValues();
var emailvalid = await emailValidation(currentValues['email']); //Ajax call to external provider
if ( !emailvalid ) {
mktoForm.submittable(false);
}
}
});
const emailValidation = function (email) {
return new Promise( (resolve) => {
jQuery.get( 'https://externalemailprovider....../'+email, function( data ) {
if ( typeof data === 'undefined' || typeof data.validation === 'undefined' ) { return resolve(false); }
if ( data.validation != 'Valid' ) { return resolve(false); }
return resolve(true);
});
});
}
I would like to know if it's possible to call a promise or an async function inside the onValidate Marketo api method . The idea is once the user has filled out the form, check against an external API some fields, such as email or phone. The idea is similar to the above code.
Thank you for your help.
David
Solved! Go to Solution.
I would like to know if it's possible to call a promise or an async function inside the onValidate Marketo api method . The idea is once the user has filled out the form, check against an external API some fields, such as email or phone. The idea is similar to the above code.
Of course, but not the way you’re doing it. That await isn’t stopping the submission.
You need to set the form to submittable(false) by default. Then only set it to submittable(true) if your external API call returns success, i.e. by updating a variable in a closure.
I would like to know if it's possible to call a promise or an async function inside the onValidate Marketo api method . The idea is once the user has filled out the form, check against an external API some fields, such as email or phone. The idea is similar to the above code.
Of course, but not the way you’re doing it. That await isn’t stopping the submission.
You need to set the form to submittable(false) by default. Then only set it to submittable(true) if your external API call returns success, i.e. by updating a variable in a closure.
Hi Sandford,
great!! that makes sense at all. I didn't see that approach.
Thank you so much for your help