SOLVED

Re: Require Business Email Address on Forms

Go to solution
Trevor_Parsell
Level 6

Hello,

I found the script below in this discussion: How do I require a BUSINESS email

I pasted the script in my Marketo landing page template and tested the form with a Yahoo email address and it still is allowing me to submit the form. Where exactly should this script be placed to reject non-business email addresses for particular forms? Any help would be greatly appreciated.

<script>
(function (){
  // Please include the email domains you would like to block in this list
  var invalidDomains = ["@yahoo.","@hotmail.","@live.","@aol.","@msn.","@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>



Thanks!

Trevor

1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

Lawdy, how I hate that code.

It should have barebones functionality, though.  Are you being sure to include it after the <script> that loads forms2.min.js? On a Marketo LP, you'll want to put this just inside the closing </body> tag (because Mkto may inject the forms2.min.js at any point in the body content).

View solution in original post

35 REPLIES 35
SanfordWhiteman
Level 10 - Community Moderator

 

<script src="https://info.lacework.com/rs/016-ATL-295/images/freetxt.js"></script>

 

goes above your other scripts.

 

Then myArray will be available in later scripts.

 

By default, scripts are always executed top-to-bottom. You would find this new post informative about script loading order.

TrishVoskoSpear
Level 2

thanks! but i obviously need help as I am not understanding!! so sorry.

so i first tried adding  the line under <!-- include custom JavaScript --> section at top - no joy.

now I have as you see below under <!-- include jQuery library --> section and still no joy.

 

At this point I want to give up but hoping you can review all below and tell me what is wrong!

 

 

<!-- include jQuery library --> 
<script src="https://info.lacework.com/rs/016-ATL-295/images/freetxt.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" defer></script> 
<script>window.jQuery || document.write('<script src="https://info.lacework.com/rs/016-ATL-295/images/jquery-3.3.1.min.js" defer><\/script>')</script> 
<!-- include compressed foundation JavaScript --> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.4.3/js/foundation.min.js" defer></script> 
<!-- include custom JavaScript --> 
<script src="https://info.lacework.com/rs/016-ATL-295/images/app.js" defer></script>
<script>
function inDomainList(email, domains) {
return domains.map(function(domain) {
return new RegExp('@' + domain.replace('.', '\\.') + '$', 'i');
}).some(function(reDomain) {
return reDomain.test(email);
});
}
MktoForms2.whenReady(function(form) {
var freemailDomains = ["yahoo.com", "hotmail.com", "live.com", "aol.com", "msn.com", "outlook.com", "gmail.com"],
errorFreemail = "Must be Business email."; /* no need to touch below this line */
var formEl = form.getFormElem()[0],
emailEl = formEl.querySelector("input[name='Email']"),
emailJq = MktoForms2.$(emailEl);
form.onValidate(function(native) {
if(!native) return;
var currentValues = form.getValues(),
currentEmail = currentValues.Email;
if(inDomainList(currentEmail, freemailDomains)) {
form.submittable(false);
form.showErrorMessage(errorFreemail, emailJq);
} else {
form.submittable(true);
}
});
});
</script>
</body>
</html>

 

SanfordWhiteman
Level 10 - Community Moderator

Immediate/side note: the way jQuery is being loaded there is quite horrifying, sort of want to shake whoever came up with that...

 

... but anyway, that's not related to the email validation, nor is your app.js (which is deferred, so couldn't have any effect here even if you wanted it to).

 

When you load freetxt.js, you're creating an array called myArray.

 

So myArray is what you'd use instead of freemailDomains. You're creating the new array with the big list, but still using the original array, in other words.

TrishVoskoSpear
Level 2

I have to give up - will need to find a developer to just create the final code for me as i still can't get to work - LOL

 

I replaced all instances of "freeDomains" with "myArray" but no joy.

 

 

SanfordWhiteman
Level 10 - Community Moderator
What page is this on? You seem to have gotten very close.
TrishVoskoSpear
Level 2

@SanfordWhiteman 

https://info.lacework.com/Business-Email-Test---Trish.html

 

and with this update now even the ones i listed out that did work don't work any more (yahoo.com etc)

 

Thanks,

Trish

SanfordWhiteman
Level 10 - Community Moderator

You can't redeclare myArray. That's overwriting (shadowing, technically, but in practice the same as overwriting) the whole list you loaded, so you're back to the original list.

 

This dual declaration

var myArray = ["yahoo.com", "hotmail.com", "live.com", "aol.com", "msn.com", "outlook.com", "gmail.com"],
errorFreemail = "Must be Business email.";

needs to be only

var errorFreemail = "Must be Business email.";

 

 


and with this update now even the ones i listed out that did work don't work any more (yahoo.com etc)

It does block the ones in the original list, not sure what you mean here.

Trish_ITRenew
Level 1

Yay all working now!

SanfordWhiteman
Level 10 - Community Moderator
Great.
TrishVoskoSpear
Level 2

I will try it!

TrishVoskoSpear
Level 2

haha - wasn't me. I have inherited these templates!

TrishVoskoSpear
Level 2

@SanfordWhiteman 

Ah - thanks for the tip!  Here is code I am using (stolen from you 😁) to list specific domains to not allow on forms. Do i add code to this to include the long list on my free.txt file I have uploaded to Images and Files? Or is it a complete different block of code I need to add to my templates. 

<script > function inDomainList(email, domains) {
    return domains.map(function(domain) {
        return new RegExp('@' + domain.replace('.', '\\.') + '$', 'i');
    }).some(function(reDomain) {
        return reDomain.test(email);
    });
}
MktoForms2.whenReady(function(form) {
    var freemailDomains = ["yahoo.com", "hotmail.com", "live.com", "aol.com", "msn.com", "outlook.com", "gmail.com"],
        errorFreemail = "Must be Business email."; /* no need to touch below this line */
    var formEl = form.getFormElem()[0],
        emailEl = formEl.querySelector("input[name='Email']"),
        emailJq = MktoForms2.$(emailEl);
    form.onValidate(function(native) {
        if (!native) return;
        var currentValues = form.getValues(),
            currentEmail = currentValues.Email;
        if (inDomainList(currentEmail, freemailDomains)) {
            form.submittable(false);
            form.showErrorMessage(errorFreemail, emailJq);
        } else {
            form.submittable(true);
        }
    });
}); < /script>

 

 

Thanks for all your help!

TrishVoskoSpear
Level 2

hmm, now code in the previous format  i inserted (with line breaks) not working but working when all on one line like i copied from your post:

 

<script>function inDomainList(email,domains){  return domains    .map(function(domain){      return new RegExp('@' + domain.replace('.','\\.') + '$','i');    })    .some(function(reDomain){      return reDomain.test(email);    });}MktoForms2.whenReady(function(form){  var freemailDomains = ["yahoo.com","hotmail.com","live.com","aol.com","msn.com","outlook.com","gmail.com"],      errorFreemail = "Must be Business email.";  /* no need to touch below this line */  var formEl = form.getFormElem()[0],      emailEl = formEl.querySelector("input[name='Email']"),      emailJq = MktoForms2.$(emailEl);  form.onValidate(function(native){    if (!native) return;    var currentValues = form.getValues(),        currentEmail = currentValues.Email;    if ( inDomainList(currentEmail, freemailDomains) ) {      form.submittable(false);      form.showErrorMessage(errorFreemail, emailJq);    } else {      form.submittable(true);    }  });});</script>

 

Katherine_Nguye
Level 1

Hi Sanford,

Thanks for the insightful information! I was trying to look for a solution that solves for the concerns you shared.

So, I've included this in a Marketo LP and ran a few tests. It's properly blocking out "gmail.com," "aol.com," etc in line 14. But, it's not blocking emails from that larger list you shared, from freemail/free.txt at master · willwhite/freemail · GitHub 

Can you advise on how we can block all emails seen in that list?

Thank you!

SanfordWhiteman
Level 10 - Community Moderator

First you'd need to make sure the free.txt file is uploaded to Design Studio (for forms on Marketo LPs) and/or to your corporate site (for embedded forms).