0
    function AllowOnlyNumbers(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57)){
            if (charCode === 8 && charCode === 46) {
                return false;
            }
        }
        return true;
    }

How to allow only numbers and delete key or backspace to be written in this textbox ?

2 Answers2

0

Why not use input of type number <input type="number" /> for browsers that supports it, otherwise use javascript:

function AllowOnlyNumbers(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}

Here is a fiddle: http://jsfiddle.net/Lm2hS/

From: https://stackoverflow.com/a/7295864/235659

Anas
  • 5,622
  • 5
  • 39
  • 71
  • Thanks for answer, I used `` It not wored –  Aug 26 '18 at 05:03
  • I added a fiddle, it works fine, but this depends on your browser, I just tried in Chrome, it works fine but doesn't work in Safari for example. So it depends on what browsers you're targetting. – Anas Aug 26 '18 at 05:07
  • I use firefox and what is fiddle. –  Aug 26 '18 at 05:12
  • Thanks for answer, only `del` key not worked. but `backspace` key worked. –  Aug 26 '18 at 05:31
0

I have created this solution for your problem here:

https://codebrace.com/editor/b05f92054

Here I have used

event.charCode == 0
to allow non characters key pressed (To allow delete, backspace and other non-character keys) and
isNaN
to check if the value entered is a number or not. I hope this helps!