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 > 140) return 'body has a 140 character limit';
return true;
},
username: function(val) {
if (val.length > 20) return 'username has a 20 character limit';
return true;
},
password: function(val) {
if (val.length < 8 && val.length > 16) return 'passward has a 8-16 character limit';
return true;
},
email: function(val) {
// RegExp: http://blog.livedoor.jp/dankogai/archives/50954045.html
if (!/^(?:(?:(?:(?:[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))
return 'invalid Email address';
return true;
}
};
$.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