Limit Email Domains on Form Submission | Community
Skip to main content
Jon_Jagelsky
Level 2
February 3, 2014
Solved

Limit Email Domains on Form Submission

  • February 3, 2014
  • 20 replies
  • 6197 views
We used the java script from this article on all of our landing pages with 1.0 forms in order to block personal email domains but in testing I'm finding that this same script does not work with new forms 2.0.

https://community.marketo.com/MarketoResource?id=kA650000000GtqvCAC

Anyone else running into this same issue or have any suggestions on how to block email domains with Forms 2.0?
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 Kathi_Gosche

One of my developers came up with this code that has worked for me. Depending on the way your button is designed, it may or may not work for you, but you can give it a try.

Replace your old validation code with the below:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
var $jQ = jQuery.noConflict();
var invalidDomains =
[
"hotmail.com",
"yahoo.com",
"aol.com",
"gmail.com",
];
$jQ(document).ready(function(){
if ($jQ('.mktoButtonRow').length == 0) {
window.setTimeout(function(){
emailValidation();
}, 500);
} else {
emailValidation();
}
});
$jQ('#Email').bind('blur', function(){
if (isEmailValid()){
var $error = $jQ(this).parent().find('.mktoError');
if ($error.length > 0) {
$error.fadeOut(200, function(){
$error.remove();
$jQ('#Email').removeClass('mktoInvalid');
});
}
}
});
function emailValidation() {
var $buttonRow = $jQ('.mktoButtonRow');
var submitText = $buttonRow.find('button').html();
var $replacement = $jQ('<input id="replacementButton" type="button" class="mktoButton" value="' + submitText + '" />');
$buttonRow.hide();
var buttonRowContents = $buttonRow.html();
$span = $jQ(buttonRowContents).html('');
var $replacementRow = $jQ('<div class="mktoFormRow" />').append($span.append($replacement));
$jQ('.mktoFormRow :last').after($replacementRow);
$jQ('#replacementButton').click(function(){
console.log('debug1');
if (!isEmailValid()) {
console.log('debug2');
var $error = $jQ('#Email').parent().find('.mktoError');
if ($error.length == 0) {
$error = getError('Please provide a business or institutional email address');
$jQ('#Email').after($error);
}
$error.fadeIn(200);
$jQ('#Email').removeClass('mktoInvalid').addClass('mktoInvalid');
return;
} else {
console.log('debug3');
$jQ('.mktoButtonRow button').trigger('click');
}
});
}
function isEmailValid() {
var email = $jQ('#Email').val().toLowerCase();
for(i=0; i < invalidDomains.length; i++) {
var invalidDomain = invalidDomains[i].toLowerCase();
if (email.indexOf(invalidDomain) !== -1) {
return false;
}
    }
return true;
}
function getError(message, detail) {
var $container = $jQ('<div class="mktoError" style="right: 0px; bottom: -47px; display: none" />');
var $arrow = $jQ('<div class="mktoErrorArrowWrap"><div class="mktoErrorArrow"></div></div>');
var $errorMessage = $jQ('<div class="mktoErrorMsg" />').html(message);
if (typeof detail == 'string' && $jQ.trim(detail) != '') {
$errorMessage = $errorMessage.append($jQ('<span class="mktoErrorDetail" />').html(detail))
}
return $container.append($arrow).append($errorMessage);
}
</script>

20 replies

Enget_Dang
Level 3
April 1, 2014
I've had trouble with this script from IE as well. Caused some big headaches when attendees were trying to register for a webinar and couldn't submit their information.

Worked with our web developer to get this script working. Feel free to try and test. Used BrowserStack to test across browsers using the Marketo embed script. Insert after loadForm line:
 

form.onValidate(function(valid) {
var email = form.getFormElem().find("#Email");
 
var validateEmail = function(email) {
var filters = [
"gmail", "yahoo"
],
domain = email.substring(email.indexOf('@') + 1),
parts = domain.split('.');
 
for(var filter in filters) {
for(var i = 0, ln = parts.length; i < ln; i++) {
if(parts[i].toLowerCase() == filters[filter]) {
return false;
}
}
}
return true;
};
 
if(validateEmail(email.val())) {
form.submitable(true);
} else {
form.submitable(false);
form.showErrorMessage("Please provide a company email address.", email);
}
});
 
});</script>
Enget_Dang
Level 3
April 1, 2014
Also, this is not an April Fool's joke ;)
Jon_Jagelsky
Level 2
May 2, 2014
Hi Enget,

Thanks for sharing your code! I finally got around to testing last week and it worked as expected when embedding the form. I'm curious if you've had any success using this when placing the form directly on a Marketo LP? and how you got it to work? I tried using it with the drag and drop form and the script was causing the form to load twice on the page.

The problem is when using this and embeding the form I've lost the prefill functionality. 

I hate to gain one functionality but lose another. Would love to hear your thoughts.

Thanks!
Jason_Scott
Level 4
July 24, 2014
Jon or Enget,

Would either one of you be willing to share a live page with this script?

Thanks!
Jason
Enget_Dang
Level 3
July 24, 2014
Hi Jon and Jason,

I haven't been able to get the script to work on a Marketo LP, only as an embedded form on a web page. Currently working on a solution to get it resolved on Marketo LPs. Jon, here is the form we are using with the current script: http://www.boaweb.com/contact-us/ 

Thanks!

Enget
Jason_Scott
Level 4
July 24, 2014
Thanks Enget!
July 31, 2014
Has anyone had an issue with getting the form.showErrorMessage() to persist when users hit the ENTER key instead of the submit button?

For us, when users uses the ENTER key to submit the form, the message is rendered but quickly, but quickly disappears.  If the user clicks the SUBMIT button, the error message persists until the user edits the input box (correct behavior).

Any ideas how to make the ENTER key and SUBMIT button behave the same?

We've tested this only on a Mac, but same behavior exists in Safari, Firefox and Chrome.
Hiram_Cruz
Level 2
October 8, 2014
Hi group, 

I'm trying to embed a form on a non-marketo LP with the javascript from this thread to exclude gmails & yahoo's ect.

I copy + pasted Egnet's code but can't get the form to render. Can someone please help point out what I'm doing wrong here? 

Here's my script: 



<script src="//learn.cpcstrategy.com/js/forms2/js/forms2.js"></script>
<form id="mktoForm_1200"></form>
<script>MktoForms2.loadForm("//learn.cpcstrategy.com", "006-GWW-889", 1200, function(form){
 
form.onValidate(function(valid) {
var email = form.getFormElem().find("#Email");
 
var validateEmail = function(email) {
var filters = [
"123mail","aol","att","bellsouth","charter","comcast","cox","earthlink","gmail","gmx","gmx","googlemail","hotmail","juno","live","mac","mail","me","mindspring","msn","optonline","pacbell","rediffmail","rocketmail","rogers","rr","sbcglobal","sympatico","telus","verizon","web","yahoo","ymail","zigmail","bigstring","bumpymail","centermail","centermail","discardmail","dodgeit","e4ward","emailias","fakeinformation","front14.","getairmail","ghosttexter","jetable","kasmail","link2mail","mailexpire","mailinator","mailmetrash","mailmoat","messagebeamer","mytrashmail","nepwk","nervmich","netmails","netzidiot","nurfuerspam","oneoffemail","pookmail","privacy","punkass","rmqkr","sharklasers","sneakemail","sofortmail","sogetthis","spam","spambob","spambob","spambob","spambog","spamex","spamgourmet","spamhole","spaminator","spammotel","spamtrail","trash-mail","trashymail","trashmail","yopmail","wuzup"
],
domain = email.substring(email.indexOf('@') + 1),
parts = domain.split('.');
 
for(var filter in filters) {
for(var i = 0, ln = parts.length; i < ln; i++) {
if(parts[i].toLowerCase() == filters[filter]) {
return false;
}
}
}
return true;
};
 
if(validateEmail(email.val())) {
form.submitable(true);
} else {
form.submitable(false);
form.showErrorMessage("Please provide a company email address.", email);
}
 
});
});
</script>
 





January 20, 2015
This code works great, but is there a way to edit it so that the button is red instead of blue?
Jason_Scott
Level 4
January 29, 2015
You can edit button when building the form in Marketo.