Among all browser few browsers use keyCode, others use which. If you’re using jQuery, you can reliably use which. Method 1: The below JavaScript code can be used to validate user input to accept only numbers:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<html>

<head>

<script type="text/Javascript">

functiononlyNumbers(e,t){

try{

if(window.event){

varcharCode=window.event.keyCode;// This will set "charCode" to a numeric keyCode of the input(keypress) that triggers the event.