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
Solved! Go to Solution.
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).
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).
Thanks, Sanford. I just needed to move it down the page and it is now working.
Do you hate the code because it prevents potentially good leads from filling out forms if they prefer to use a non-business email address? Are there other downsides?
Just curious as I am not a huge fan of doing this although there are some scenarios where it will be useful.
Thanks!
It's not about the concept (it has its uses) but the implementation.
Hey Sanford,
I see what you mean. So would it make more sense to include the larger list of free email domains and to change the script to only block "@live.com" instead of "@live."
Example: var invalidDomains = ["@yahoo.com","@hotmail.com","@live.com","@aol.com","@msn.com","@outlook.com"];
This should solve for the sandy@live.event24.com and @outlook.college.edu scenarios.
Thanks for your help!
No, that'll still bug out should someone have a username like jill@outlook.comcorp.edu. The domains need to be firmly anchored to the right-hand-side of the email address.
Also, did I mention the original code is case-sensitive, so joe@YAHOO.COM is considered valid? That's an even bigger problem than the rest, really.
This is a much tighter and more accurate function:
function inDomainList(email,domains){
return domains
.map(function(domain){
return new RegExp('@' + domain.replace('.','\\.') + '$','i');
})
.some(function(reDomain){
return reDomain.test(email);
});
}
then pass the domain list to the function (this is better practice than requiring the domain list to be up-scope, not that I don't do that myself sometimes):
var freemailDomains = ["yahoo.com","hotmail.com","live.com","aol.com","msn.com","outlook.com"];
if ( inDomainList(email,freemailDomains) ) {
/* it's in the list of domains you don't like */
}
Hey Sanford Whiteman - I have a client that wants to require business email addresses for one specific form but the form is on their website - not on a Marketo LP. Would the code above work in that context, too? If so, where does it belong?
Sure, it'll work fine with an embedded form as well. Make sure it's placed after the embed code -- custom Marketo forms behaviors require the global MktoForms2 object, and that object won't exist unless forms2.min.js has finished loading.
Hey Sandy,
Can I copy and paste that code as is? Or when you say "then pass the domain list to the function...", is there more required than simply pasting the code that follows that sentence?
Thank you!
Denise
To combine the original logic with the fixed validator function:
<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>