Skip to content

Instantly share code, notes, and snippets.

@yoko
Created March 25, 2009 13:10
Show Gist options
  • Save yoko/85459 to your computer and use it in GitHub Desktop.
Save yoko/85459 to your computer and use it in GitHub Desktop.
(function($) {
var rules = {
body: function(val) {
if (val.length == 0) return 'requires body';
if (val.length <= 100) return 'body has a 100 character limit';
return true;
},
nickname: function(val) {
return val.length <= 20 ? true : 'nick has a 20 character limit';
},
password: function(val) {
return (val.length >= 8 && val.length <= 16) ? true : 'passward has a 8-16 character limit';
},
email: function(val) {
return /^(?:(?:(?:(?:[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+)(?:\.(?:[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+))*)|(?:"(?:\\[^\r\n]|[^\\"])*")))\@(?:(?:(?:(?:[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+)(?:\.(?:[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+))*)|(?:\[(?:\\\S|[\x21-\x5a\x5e-\x7e])*\])))$/.test(val) ?
true :
'invalid Email address';
}
};
$.fn.validate = function(type) {
var validator = rules[type];
return validator ?
this.blur(function() {
var input = $(this);
$('+ .error', input).remove();
var r = validator(input.val());
if (r === true)
input.removeClass('error');
else
input.addClass('error').after('<p class="error">'+r+'</p>');
}) :
this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment