edgar.dueñas
Enviado por edgar.dueñas el Mié, 11/05/2016 - 11:38

Este script valida el ingreso de únicamente números en un campo de texto, obligando al usuario a ingresar únicamente números en un campo de tipo texto.

/* START DOCUMENT READY */
(function($) {
    $(document).ready(function() {
 
        $("#edit-amount").keydown(function(e) {
            var keyPressed;
            if (!e)
                var e = window.event;
            if (e.keyCode)
                keyPressed = e.keyCode;
            else if (e.which)
                keyPressed = e.which;
            var hasDecimalPoint = (($(this).val().split('.').length - 1) > 0);
            if (keyPressed == 46 || keyPressed == 8 || ((keyPressed == 190 || keyPressed == 110) && (!hasDecimalPoint)) || keyPressed == 9 || keyPressed == 27 || keyPressed == 13 ||
                    // Allow: Ctrl+A
                            (keyPressed == 65 && e.ctrlKey === true) ||
                            // Allow: home, end, left, right
                                    (keyPressed >= 35 && keyPressed <= 39)) {
                        // let it happen, don't do anything
                        return;
                    }
                    else {
                        // Ensure that it is a number and stop the keypress
                        if (e.shiftKey || (keyPressed < 48 || keyPressed > 57) && (keyPressed < 96 || keyPressed > 105)) {
                            e.preventDefault();
                        }
                    }
 
                });
 
    });
})(jQuery);