Async validation to external email provider | Community
Skip to main content
New Participant
October 26, 2022
Solved

Async validation to external email provider

  • October 26, 2022
  • 1 reply
  • 1706 views

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.
Best answer by SanfordWhiteman

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.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
October 27, 2022

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.

davkapturAuthor
New Participant
November 2, 2022

Hi Sandford,

 

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

 

Thank you so much for your help