Skip to content

Instantly share code, notes, and snippets.

@mikeyledoux
Created March 8, 2012 01:56
Show Gist options
  • Save mikeyledoux/1997990 to your computer and use it in GitHub Desktop.
Save mikeyledoux/1997990 to your computer and use it in GitHub Desktop.
FormValidator Fixer Class for Magento forms in IE7 & IE9. Works with out-of-box Magento JS. Thanks to Tom Rosario for organizing this into a comprehensive object.
/* FormValidator Fix for IE7 & IE9 */
var ie7 = $j.browser.msie && $j.browser.version=="7.0";
var ie9 = $j.browser.msie && $j.browser.version=="9.0";
var safari =$j.browser.safari;
/*--------------------------------------------------------------------------*/
var FormValidator = function (form, opts) {
this.form = $j(form);
this.required = form.find('.required-entry');
this.defaults = {
email: form.find('.validate-email')
};
this.options = $j.extend({}, this.defaults, opts);
this.setup();
};
FormValidator.prototype = {
setup: function () {
var self = this;
this.form.submit(function () {
self.onSubmit();
});
},
validateEmail: function () {
var emailCheck = this.options.email.val().match(/^([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9,!\#\$%&'\*\+\/=\?\^_`\{\|\}~-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*@([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z0-9-]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*\.(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]){2,})$/i);
if (emailCheck != null) { return true; }
else { return false; }
},
onSubmit: function () {
var self = this, numFilled = 0;
this.required.each(function (idx) {
if ($j(this).val().length > 0) {
numFilled++;
}
});
var valid = this.validateEmail() && (numFilled == this.required.length);
if (valid) {
this.form.removeAttr('onsubmit');
this.form.submit();
return true;
} else {
/*uncomment the alert below to test your numFilled variable
if all is going well it shouldn't incriment and match the
number of required inputs you have*/
//alert(numFilled);
}
}
};
// [Call function]
if(ie7 || ie9) { new FormValidator($j('#login-form'));}
else {$j('#login-form').removeAttr('onsubmit')}
// [HTML]: <form action="[magento action]" onsubmit="return false">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment