// fairly basic validation plugin will validate all input with a required attribute (function ($) { var _methods = { init: function(o){ var s = $.extend({ emailPattern: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, decimalPattern: /^[0-9]+([\.][0-9]{1,2})?$/, numberPattern: /^[0-9]+$/, datePattern: /(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)\d\d/, urlPattern: /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/, errorClass: 'error', showErrorMessage: true }, o); // sets the validation type & whether they are enabled var _validationTypes = { required: { enabled: true, func: ValidRequired, errorMessage: "This field is required" }, email: { enabled: true, func: ValidEmail, errorMessage: "Invalid email address" }, decimal: { enabled: true, func: ValidDecimal, errorMessage: "Not a valid number" }, number: { enabled: true, func: ValidNumber, errorMessage: "Not a valid number" }, date: { enabled: true, func: ValidDate, errorMessage: "Invalid date" }, url: { enabled: true, func: ValidUrl, errorMessage: "Invalid URL" } } var validated = true; var $wrapper = $(this); _clearValidation($wrapper); $('input[validate]', $(this)).each(function(){ var $input = $(this); var isValid = true; $input.removeClass('error'); // build an array from all the validation types var inputValArray = $input.attr('validate').split(' '); // if the input isn't required but is blank then it has passed if(($.inArray('required',inputValArray) == -1) && $input.val() == ''){ return true; } $.each(inputValArray, function(index, type){ if(_validationTypes[type].enabled){ // run the appropriate validation method!!! // leave the each if there are any failures isValid = _validationTypes[type].func($input); if(!isValid){ $input.addClass(s.errorClass); if(s.showErrorMessage){ $errorText = $('').addClass(s.errorClass).html(_validationTypes[type].errorMessage); $input.after($errorText); } } } return isValid; }); if(!isValid){ validated = false; } }) function ValidRequired($this) { //var validRequired = (($.trim($this.val()) != '') || ($this.val() == $this.attr('placeholder'))); var validRequired = (($.trim($this.val()) != '') && ($this.val() != $this.attr('placeholder'))); // steve, 04/09/2012, this was wrong! return validRequired; } function ValidEmail($this) { var validEmail = s.emailPattern.test($this.val()); return validEmail; } function ValidDate($this) { var validDate = s.datePattern.test($this.val()); return validDate; } function ValidDecimal($this) { var validDecimal = s.decimalPattern.test($this.val()); return validDecimal; } function ValidNumber($this) { var validNumber = s.numberPattern.test($this.val()); return validNumber; } function ValidUrl($this) { var validUrl = s.urlPattern.test($this.val()); return validUrl; } return validated; }, clear: function(){ _clearValidation($(this)); } }; function _clearValidation($this){ $('.error', $this).remove('span').removeClass('error'); } $.fn.validate = function (method) { if(_methods[method]){ return _methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); } else if(typeof method === 'object' || !method){ return _methods.init.apply(this, arguments) } else{ $.error('Method ' + method + ' does not exist on jQuery.senior.validate'); } } })(jQuery);