Numeric Validation with paste and Shift

Numeric Validation: Accept only numeric values

by Ankit Patidar

HTML

<input type="text" class="numeric">
    <span class="error" style="color: Red; display: none">* Input digits (0 - 9)</span>

JavaScript

var specialKeys = new Array();
        specialKeys.push(8,9,37,39,46); //Backspace,Tab,Arrow,Delete
        $(function () {
            $(".numeric").bind("keypress", function (e) {
                var keyCode = e.which ? e.which : e.keyCode
                var ret = ((keyCode >= 48 && keyCode <= 57 || keyCode == 222) || specialKeys.indexOf(keyCode) != -1);
                $(".error").css("display", ret ? "none" : "inline");
                return ret;
            });
            $(".numeric").bind("paste", function (e) {
                return false;
            });
            $(".numeric").bind("drop", function (e) {
                return false;
            });
        });