Métodos adicionais para jQuery

by gsguma

JavaScript

/**
 * Metodo: 
 * Finalidade: Permite apenas numeros no campo
 * Observacao: Aceita apenas: backspace, tab, delete, * setas <- e ->, numeros e por erro "%"
 */
jQuery.fn.onlyNumbers = function(){
    return this.each(function(){
        jQuery(this).bind('keydown keypress keyup',function(e){
            var key = e.charCode || e.keyCode || 0;
            return (key==13||key==8||key==9||key==46||key==35||key==36||key==37||key==39||(key>=48&&key<=57)||(key>=96&&key<=105));
        });
    });
}

/**
 * Metodo: exists
 * Finalidade: Verifica se o element existe
 */
jQuery.fn.exists = function() {
    return jQuery(this).length > 0;
}

/**
 * Metodo: scrollTo
 * Finalidade: Rola a pagina com efeito ate o elemento
 */
jQuery.fn.scrollTo = function() {
    $('html, body').animate({ scrollTop: $(this).exists() ? $(this).offset().top : 0 }, 'slow');
    return this;
}

/**
 * Metodo: resetForm
 * Finalidade: Reseta os campos do formulario e posiciona o foco no primeiro campo
 */
jQuery.fn.resetForm = function() {
    var el = jQuery(this).attr('id');
    jQuery(this).each(function() { this.reset(); });
    jQuery(el+" :text:visible:enabled").filter(function() {
        return jQuery(this).parents().filter(function() { return this.style.display == "none"; }).size() == 0;
    })
    .eq(0)
    .focus();    
    return this;
}

/**
 * Metodo: focusFirst
 * Finalidade: Posiciona o foco no primeiro campo do form
 */
jQuery.fn.focusFirst = function() {
    var el = jQuery(this).attr('id');
    jQuery(el+" :text:visible:enabled").filter(function() {
        return jQuery(this).parents().filter(function() { return this.style.display == "none"; }).size() == 0;
    })
    .eq(0)
    .focus();    
    return this;
}

/**
 * Metodo: showError
 * Finalidade: Mostra uma mensagem de erro com bordas vermelhas
 * Parametros:
 *     message: mensagem a ser mostrada no elemento
 */
jQuery.fn.showError = function(message) {
   ...