Adding Script to a Marketo Embed Code | Community
Skip to main content
June 12, 2018
Solved

Adding Script to a Marketo Embed Code

  • June 12, 2018
  • 2 replies
  • 4681 views

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>
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

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.

2 replies

Jay_Jiang
New Participant
June 14, 2018

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
New Participant
June 14, 2018

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
New Participant
June 14, 2018

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
SanfordWhitemanAccepted solution
New Participant
June 12, 2018

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.

June 13, 2018

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?