Skip to content

Instantly share code, notes, and snippets.

@garethpbk
Created September 10, 2018 16:44
Show Gist options
  • Save garethpbk/0bf7745f5ca9c8b595ec56768c12d483 to your computer and use it in GitHub Desktop.
Save garethpbk/0bf7745f5ca9c8b595ec56768c12d483 to your computer and use it in GitHub Desktop.
// from https://codepen.io/Evangogh/pen/VjaRQV
MktoForms2.loadForm("//app-ab06.marketo.com", "981-LQX-968", 3668);
MktoForms2.whenReady(function(form) {
form.onValidate(function(builtInValid) {
if (!builtInValid) return;
form.submittable(true);
var vals = form.vals();
var invalidDomains = ["gmail.cm", "gmail.co",
"gmail.om", "yahoo.cm", "yahoo.co", "yahoo.om",
"hotmail.cm", "hotmail.co", "hotmail.om",
"outlook.cm", "outlook.co", "outlook.om",
"aol.cm", "aol.co", "aol.om"
],
emailExtendedValidationError =
'Please enter a valid email address.',
emailField = 'Email',
emailFieldSelector = '#' + emailField;
var invalidDomainRE = new RegExp('@(' + invalidDomains.join(
'|') + ')$', 'i');
if (invalidDomainRE.test(form.vals()[emailField])) {
form.showErrorMessage(emailExtendedValidationError,
form.getFormElem().find(emailFieldSelector)
);
form.submittable(false);
}
var emailRegExp =
/^[^\s@]+@([^\s@.,]+\.)+[^\s@.,]{2,}$/;
var validEmail = emailRegExp.test(vals.Email);
if (!validEmail) {
form.showErrorMessage(emailExtendedValidationError,
form.getFormElem().find(emailFieldSelector)
);
form.submittable(false);
}
var phoneRegExp =
/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$/g;
var validPhone = phoneRegExp.test(vals.Phone);
phoneExtendedValidationError =
'Please enter a valid phone number.',
phoneField = 'Phone',
phoneFieldSelector = '#' + phoneField;
if ((vals.Phone.length < 10) || (vals.Phone.length >
20) || (vals.Phone == "0123456789") || (vals.Phone ==
"1234567890") || (vals.Phone == "9876543210") ||
(vals.Phone == "0987654321") || (!validPhone)) {
form.submittable(false);
var businessphone = form.getFormElem().find(
"#Phone");
form.showErrorMessage(
"Please enter a valid phone number.",
businessphone);
}
var invalidPhoneRE = /^\d*?(\d)\1{5,}\d*$/;
if (invalidPhoneRE.test(form.vals()[phoneField])) {
form.showErrorMessage(phoneExtendedValidationError,
form.getFormElem().find(phoneFieldSelector)
);
form.submittable(false);
} else {
form.submittable(form.submittable() !== false);
}
});
form.onSubmit(function(form) {
alert("Success!");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment