Skip to content

Instantly share code, notes, and snippets.

@maurobaraldi
Created March 23, 2011 21:33
Show Gist options
  • Save maurobaraldi/884057 to your computer and use it in GitHub Desktop.
Save maurobaraldi/884057 to your computer and use it in GitHub Desktop.
/**
* Submittr v1.0
* jQuery plugin for form validation
* by Arthur <arthur@corenzan.com.br>
*
* Usage:
*
* $(':submit').submittr();
*
* Optional parameters:
*
* template: string, the HTML template for error messages
* after: bool, when true error messages will be palced after <input>
* rules: hash, rules for validation
*
* Rules syntax:
*
* 'input': {
* re: /.+/g,
* error: 'Input is invalid'
* }
*
* 'input' is name reference of the input which value will be
* tested against given regular expression, 're' is the reges
* itself and 'error' is the error message it will display.
*
* Optionally you can define a 'cleanup' regex that will wipe
* matched characteres from input's value.
*
* 'cleanup': /\D+/g
*
* The example above will remove non-digit characters.
*/
(function($) {
$.fn.submittr = function(options) {
var CID = 'submittr-message';
options = $.extend({
template: '<span class="error">%s</span>',
after: false,
rules: {}
}, options);
var submittr = function(event) {
event.preventDefault();
var valid = true,
submit = $(this),
form = submit.parents('form');
//remove previous errors
$(CID, form).remove();
form.find(':text, :radio, :checkbox, select, textarea').each(function(i, input) {
input = $(input);
var message, value, rule;
if(typeof (rule = options.rules[input.attr('name')]) !== 'undefine') {
if(typeof rule.re === 'undefine' || rule.message === 'undefine') {
//no rule, or rule lacking required information: skip
return true;
}
}
value = input.val();
//check cleanup
if(rule.cleanup) {
input.val(function(i, value) {
return (value + '').replace(rule.cleanup, '');
});
}
if(!value.match(rule.re)) {
message = $(options.template.replace('%s', rule.error)).addClass(CID);
input.focus()[options.after ? 'after' : 'before'](message);
valid = false;
}
return valid;
});
if(valid) {
submit.unbind('click');
form.submit();
}
};
return this.each(function(i, n) {
$(n).click(submittr);
});
};
})(jQuery);
/**
* Or, the compiled version:
*/
(function(a){a.fn.submittr=function(c){c=a.extend({template:'<span class="error">%s</span>',after:!1,rules:{}},c);var i=function(b){b.preventDefault();var g=!0;b=a(this);var h=b.parents("form");a("submittr-message",h).remove();h.find(":text, :radio, :checkbox, select, textarea").each(function(b,e){e=a(e);var f,d;if(typeof(d=c.rules[e.attr("name")])!=="undefine")if(typeof d.re==="undefine"||d.message==="undefine")return!0;f=e.val();d.cleanup&&e.val(function(a,b){return(b+"").replace(d.cleanup,"")});
f.match(d.re)||(f=a(c.template.replace("%s",d.error)).addClass("submittr-message"),e.focus()[c.after?"after":"before"](f),g=!1);return g});g&&(b.unbind("click"),h.submit())};return this.each(function(b,c){a(c).click(i)})}})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment