Accept only numbers from user using javascriptNo ratings yet.

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.

This checks that the key pressed is numerical(It is checking that the keyCode is between 48 and 57).
It will evaluate to true for any character that is not a number.
This code should be effective on all type of keyboards. the script checks whether the pressed key(input character) is numeric or not.
i.e. ASCII value not between 31 and 48 also not greater than 57(i.e. between 48 and 57), which are numbers. This is done as soon as the user press the key, the Key code is identified.Method 2:

The below JavaScript code can be used to validate user input to accept numbers with one decimal values.