/** * Simple validation script based on the idea from * GeekTantra at http://www.geektantra.com/projects/jquery-form-validate/ * * Paulo Abrantes * 23-09-2009 */ (function(jQuery){ jQuery.fn.validate = function(options){ options = jQuery.extend({ bindEventsTo: this, errorMessage: 'error', validationHandler: null, clearHandler: defaultClearHandler, errorHandler: defaultErrorHandler, successHandler: null }, options); var elementToBind = jQuery(options['bindEventsTo']); elementToBind.bind('blur', function(){ var elementId = jQuery(this).attr('id'); setTimeout(function(){ validateField(elementId); }, 250); }); elementToBind.focus(function() { options['clearHandler'](jQuery(this)); }); function validateField(id){ var fieldToValidate = document.getElementById(id); if (options['validationHandler'] == null) { return true; } var result = options['validationHandler'](jQuery(fieldToValidate)); if (result && options['successHandler'] != null) { options['successHandler'](jQuery(fieldToValidate)); } if (!result && options['errorHandler'] != null) { options['errorHandler'](jQuery(fieldToValidate), options['errorMessage']); } } }; })(jQuery); function defaultClearHandler(element) { var submitButton = jQuery(element).parents("form").children("input[type=submit]:first"); submitButton.removeAttr('disabled'); submitButton.removeClass('disabled'); jQuery(element).parents("td").next("td:last").fadeOut(300); } function defaultErrorHandler(element, message) { jQuery(element).parents("td").next("td:last").fadeIn(300).html('' + message + ''); // var submitButton = jQuery(element).parents("form").children("input[type=submit]:first"); // submitButton.attr('disabled','true'); // submitButton.addClass('disabled'); }