SOLVED

Re: Adding Script to a Marketo Embed Code

Go to solution
Anonymous
Not applicable

Adding Script to a Marketo Embed Code

I am using a wordpress page and adding a Marketo embed code. I'd like to restrict the email addresses so that people with a gmail/yahoo address cannot fill out the form. How would I go about adding the script? Would it be on the wordpress page or between the embed code?

<script>
(function (){
  // Please include the email domains you would like to block in this list
  var invalidDomains = ["@gmail.","@yahoo.","@hotmail.","@live.","@aol.","@outlook."];
  MktoForms2.whenReady(function (form){
   form.onValidate(function(){
   var email = form.vals().Email;
   if(email){
   if(!isEmailGood(email)) {
   form.submitable(false);
   var emailElem = form.getFormElem().find("#Email");
   form.showErrorMessage("Must be Business email.", emailElem);
  }else{
   form.submitable(true);
  }
  }
  });
  });
 
  function isEmailGood(email) {
   for(var i=0; i < invalidDomains.length; i++) {
   var domain = invalidDomains[i];
   if (email.indexOf(domain) != -1) {
   return false;
  }
  }
   return true;
  }
})();
</script>
Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Adding Script to a Marketo Embed Code

Scripts that make use of the MktoForms2 object need to load after the <script> that loads forms2.min.js. That can mean directly after the standard embed code, but never before.

This code is pretty broken because it will (erroneously) stop sandy@live.marketo.com and won't stop sandy@GMAIL.com (it is case-sensitive while domain names are in fact case-insensitive).

So I would recommend using the improved code that I've supplied in the past -- search my posts.

View solution in original post

9 REPLIES 9
SanfordWhiteman
Level 10 - Community Moderator

Re: Adding Script to a Marketo Embed Code

Scripts that make use of the MktoForms2 object need to load after the <script> that loads forms2.min.js. That can mean directly after the standard embed code, but never before.

This code is pretty broken because it will (erroneously) stop sandy@live.marketo.com and won't stop sandy@GMAIL.com (it is case-sensitive while domain names are in fact case-insensitive).

So I would recommend using the improved code that I've supplied in the past -- search my posts.

Anonymous
Not applicable

Re: Adding Script to a Marketo Embed Code

Thanks for this. I haven't been able to find the code you provided. However, I am using the code above and adding it after the embed code and still personal emails aren't being blocked. Any thoughts?

Jay_Jiang
Level 10

Re: Adding Script to a Marketo Embed Code

Your code is a bit messy but try this:

Edited:

MktoForms2.loadForm("https://app-sj02.marketo.com", "123-YEF-028", 2209, function(form) {

    var z = ["@hotmail.co","@gmail.com","@yahoo.co","@live.co","@aol.co","@outlook.co"];

    var e = form.getFormElem().find("#Email");

    form.onValidate(function() {

        var em = form.vals().Email.toLowerCase();

        for(i=0; i < z.length ; i++) {

            if (em.indexOf(z[i]) != -1) {

                form.submittable(false);           

                form.showErrorMessage("Please enter a business email", e);

                break;

            } else {

                form.submittable(true);

            }

        }

    });

    e[0].addEventListener("blur", function(){ 

        form.validate(); 

    }); 

});

SanfordWhiteman
Level 10 - Community Moderator

Re: Adding Script to a Marketo Embed Code

N.B. Please add jQuery library to the header

jQuery always exists if MktoForms2 exists, so no need to load it separately: MktoForms2.$ is the bundled jQuery library.

Not that you need $ anyway, though:

e[0].addEventListener("blur", function(){

  form.validate();

});

Try to keep framework use to a minimum, because it implies that functionality depends on it, when everything can be done in standard DOM events + methods now. (A jQuery wrapper object is required by form.showErrorMessage, unfortunately.)

Matching an array of domains (i.e. EOL-anchored string matches) is most efficiently implemented via regex and Array#some.

var email = "sandy@hotmail.com",

    domains = ["@hotmail.com","@gmail.com"],

    domainMatch = domains.some(function(domain){

      var re_domain = domain.replace(/\./g,"\\.") + "$";

      return new RegExp(re_domain,"i").test(email);

    });

Jay_Jiang
Level 10

Re: Adding Script to a Marketo Embed Code

Thanks for the pointers. Haven't gotten my head around regex yet. So final would be something like this?

MktoForms2.whenReady(function (form){

    var z = ["@hotmail.co","@gmail.com","@yahoo.co","@live.co","@aol.co","@outlook.co"];

    var e = form.getFormElem().find("#Email");

        form.onValidate(function() {

            var em = form.vals().Email.toLowerCase();

            var y = z.some(function(domain){

                var x = domain.replace(".","\\.") + "$";  

                return new RegExp(x,"i").test(em);

            });

            if(y){

                form.submittable(false);           

                form.showErrorMessage("Please enter a business email", e);

            } else {

                form.submittable(true);

            }

        });

    e[0].addEventListener("blur", function(){ 

    form.validate(); 

    });

});

SanfordWhiteman
Level 10 - Community Moderator

Re: Adding Script to a Marketo Embed Code

  • You don't need to lowercase the email address, because the regex is tested case-insensitively.

  • Don't know what you're trying to do with ".co". Freemail providers haven't registered every hostname that starts with the strings "live.co" or "outlook.co" (nor could they). The address sandy@outlook.corpsecurity.net isn't a Microsoft-operated address, for example. If you want to try to catch these matches (looking away from the false positives) you might as well switch to regex literals because they will more closely express what you're looking for:

var email = "sandy@aol.co.uk",

    domains = [/@hotmail\.com$/, /@gmail\.com$/, /@aol\.co\..+/],

    domainMatch = domains.some(function(re_domain){

      return new RegExp(re_domain,"i").test(email);

    });

new RegExp(<RegExp>,"i") gets a case-insensitive RegExp based on the input RegExp, which adds some overhead but does save you from adding /i everywhere.

Jay_Jiang
Level 10

Re: Adding Script to a Marketo Embed Code

Don't know what you're trying to do with ".co". Freemail providers haven't registered every hostname that starts with the strings "live.co" or "outlook.co" (nor could they). The address sandy@outlook.corpsecurity.net isn't a Microsoft-operated address, or example.

Understood. Though the intention was to pick up outlook.com.au, outlook.com.sg, outlook.co.uk etc... regardless, the matching criteria can be edited later and improved.

Anonymous
Not applicable

Re: Adding Script to a Marketo Embed Code

So is this the code i should add to my wordpress page?

Jay_Jiang
Level 10

Re: Adding Script to a Marketo Embed Code

Add the below script with your munchkin and form id details and edit var domains with the domains you want to block.

If you want to follow Sanford's suggestion to add domains directly as a regex expressions, you'd need to make further edits.

MktoForms2.loadForm("https://app-####.marketo.com", "###-###-###", formid, function(form) {

    var domains = ["@hotmail.com","@gmail.com","@yahoo.com","@live.com","@aol.com","@outlook.com"];

    var emailEle = form.getFormElem().find("#Email");

        form.onValidate(function() {

            var e = form.vals().Email;

            var result = domains.some(function(domain){

                var re_domain = domain.replace(".","\\.") + "$";

                return new RegExp(re_domain,"i").test(e);

            });

            if(result){

                form.submittable(false);           

                form.showErrorMessage("Please enter a business email", emailEle);

            } else {

                form.submittable(true);

            }

        });

    emailEle[0].addEventListener("blur", function(){ 

    form.validate(); 

    });

});