Skip to main content
Level 1
Oct 26, 2022 2:56 PM UTC
Solved

Async validation to external email provider

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SanfordWhiteman
SanfordWhitemanAccepted solution
User Group Leader
Oct 27, 2022 12:30 AM UTC

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.

2 replies

SanfordWhiteman
User Group Leader
SanfordWhitemanUser Group LeaderAccepted solution
User Group Leader
Oct 27, 2022 12:30 AM UTC

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.

Translate
davkapturAuthor
Level 1
Nov 2, 2022 8:16 AM UTC

Hi Sandford,

 

great!! that makes sense at all. I didn't see that approach.

 

Thank you so much for your help

Translate