Javascript function that does not work

0

Good afternoon everyone what happens is the following I have a function in JavaScript which cancels letters in a textbox and does not work I have the following

        $(function () {
            $(".numbers").keydown(function (e) {
                if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 40]) !== -1 ||
                    (e.keyCode == 65 && e.ctrlKey === true) ||
                    (e.keyCode >= 35 && e.keyCode <= 39)){
                    return;
                }
                if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                    e.preventDefault();
                }
         }); 
        });

I hope you can help me thank you very much

    
asked by David Velez Mesa 17.09.2018 в 21:22
source

1 answer

1

Well, I do not see what the problem is. I have recreated the code that you have sent and it works for me ... It could be that you have to include the JQuery library.

<html>
<head>
</head>
<body>
  <input type="number" class="form-control numbers" min="0" />
</body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function () {
        $(".numbers").keydown(function (e) {
            if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 40]) !== -1 ||
                (e.keyCode == 65 && e.ctrlKey === true) ||
                (e.keyCode >= 35 && e.keyCode <= 39)){
                return;
            }
            if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                e.preventDefault();
            }
     }); 
    });
</script>
    
answered by 18.09.2018 / 09:01
source