Reputation: 101
Is it possible to intercept marketo landing page form submission with query, I am trying to run a custom form validation that will prevent the form from being submitted. any ideas?
Upvotes: 2
Views: 1605
Reputation: 28
Sandford Whiteman answered the question over at Marketo Forums;
var instanceURL = '//app-sj01.marketo.com',
munchkinId = '410-XOR-673',
formId = 734;
MktoForms2.loadForm(instanceURL, munchkinId, formId);
MktoForms2.whenReady(function(form) {
var companyURLRules = {
formFieldName : 'Website',
allowedHostnames : ['info.example.com', 'marketo.com'],
errorMessage : 'You must use an approved Domain.'
};
/* ---- NO NEED TO EDIT BELOW THIS LINE! ---- */
var formJq = form.getFormElem(),
urlJq = formJq.find('[name="' + companyURLRules.formFieldName + '"]');
form.onValidate(function(native) {
if (!native) return;
var currentVals = form.getValues();
form.submittable(false);
if (!hostnameInList(currentVals[companyURLRules.formFieldName], companyURLRules.allowedHostnames)) {
form.showErrorMessage(companyURLRules.errorMessage, urlJq);
} else {
form.submittable(true);
}
});
function hostnameInList(url, list) {
var loc = document.createElement('a');
loc.href = /^https?:\/\//.test(url) ? url : 'https://' + url;
return !!list.filter(function(itm) {
return loc.hostname == itm;
}).length;
}
});
Upvotes: 0
Reputation: 1214
This is possible. Marketo Forms provides a method called onValidate. This method lets you add custom validation that is triggered during form submission. Here is an example of how to use the onValidate method to prevent the submission of non-corporate email addresses.
<script>
(function (){
// Please include the email domains you would like to block in this list
var invalidDomains = ["@gmail.","@yahoo.","@hotmail.","@live.","@aol.","@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>
Upvotes: 2