SOLVED

Async validation to external email provider

Go to solution
davkaptur
Level 1

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

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Async validation to external email provider


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.

View solution in original post

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: Async validation to external email provider


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.

davkaptur
Level 1

Re: Async validation to external email provider

Hi Sandford,

 

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

 

Thank you so much for your help