Javascript is a mystery to me.
Why am I getting "Assertion failed at anonymous function" errors with the function below?
It seems pretty simple; it checks for the presence of a domain in the text box in the Marketo contact form and allows / disables the verification of the form.
It is assumed that the function works with a third-party script (Marketo external library) and the function on the page
MktoForms2.loadForm("http://app-sjn.marketo.com", "023-GTK-123", 4567);
which shows the contact form (for this reason JSFiddle will not work), so it can be difficult to fix it with the code that I can show here.
The main script is at http://app-sjn.marketo.com/js/forms2/js/forms2.js I obviously can't change anything. But is there something obvious in the function below? Or how does it interact with the main script? A problem (function ()?
(function (){
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.submittable(false);
var emailElem = form.getFormElem().find("#Email");
form.showErrorMessage("Must be Business email.", emailElem);
}else{
form.submittable(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;
}
})();
And this is the HTML that the script target for the document is ready:
<input style="width: 150px;"
class="mktoField mktoEmailField mktoHasWidth mktoRequired mktoValid" maxlength="255"
name="Email" id="Email" type="email">
Update 10/31/16
The error "Assertion failed", according to Bergi, seems to be a concession, and now I think that the problem should be related to the above function and how it works with the main script.