(function ($) { $.fn.liveCheck = function () { return this.each(function () { var $$ = $(this); var t = this; var url = $$.parents('form:first').attr('action'); var infospan = $$.parent().find('span'); if (!infospan.length) { infospan = $$.after(' ').next(); } $$.keyup(function () { // prevent tabbing in to the field firing an ajax hit if (t.value == '' && (t.lastValue == undefined)) return true; if (t.value != t.lastValue) { if (this.timer) clearTimeout(this.timer); infospan.removeClass('error').html(' validating...'); this.timer = setTimeout(function () { $.ajax({ url: url, data: t.name + '=' + t.value, dataType: 'json', type: 'post', success: function (j) { infospan.toggleClassIf(!j.ok, 'error').html(j.msg); } }); }, 200); t.lastValue = t.value; } }); }); }; $.fn.toggleClassIf = function (v, c) { return v ? this.addClass(c) : this.removeClass(c); }; })(jQuery);