Skip to content

Instantly share code, notes, and snippets.

@markbosky
Created April 28, 2022 16:22
Show Gist options
  • Save markbosky/2d30a3b60b66947981e53d8a7ac632ca to your computer and use it in GitHub Desktop.
Save markbosky/2d30a3b60b66947981e53d8a7ac632ca to your computer and use it in GitHub Desktop.
Marketo Email Domain Validation (Blocklist)
/*
* @author Sanford Whiteman
* @version v1.0 2020-10-22
* @copyright © 2020 Sanford Whiteman
* @license Hippocratic 2.1: This license must appear with all reproductions of this software.
* @https://nation.marketo.com/t5/product-discussions/how-do-i-get-a-form-to-display-an-error-message-in-real-time/td-p/306036
*
*
*/
(function () {
// Please include the email domains you would like to block in this list
var invalidDomains = [
"@gmail.",
"@yahoo.",
"@hotmail.",
"@live.",
"@aol.",
"@outlook."
];
var interestingEmailField = "Email";
MktoForms2.whenReady(function (mktoForm) {
let formEl = mktoForm.getFormElem()[0],
emailEl = formEl.querySelector("[name='" + interestingEmailField + "']");
mktoForm.onValidate(extendedEmailValidation);
emailEl.addEventListener("blur", extendedEmailValidation);
function extendedEmailValidation(nativeValid) {
if (nativeValid === false) return;
let currentValues = mktoForm.getValues(),
email = currentValues[interestingEmailField];
if (email) {
if (isInvalidDomain(email)) {
emailEl.classList.add("customInvalid");
emailEl.setAttribute("aria-invalid","true");
mktoForm.submittable(false);
mktoForm.showErrorMessage("Must be Business email.", MktoForms2.$(emailEl) );
} else {
emailEl.classList.remove("customInvalid");
emailEl.setAttribute("aria-invalid","false");
mktoForm.submittable(true);
}
}
}
function isInvalidDomain(email) {
return invalidDomains.some(function(domainPrefix){
return new RegExp(domainPrefix.replace(".","[.]")+"[a-z.-]+$","i").test(email);
});
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment