I have tried to create a piece of javascript that will only allow entry of numbers; BUT also allow the user to use the DELETE,TAB, arrow keys, BACKSPACE keys so they can amend the entered numbers, or move to a number with arrow key to amend, or tab to next field in the form.
The following code works in IE, but in firefox the tab,delete,backspace and arrow keys dont work.
Anyone got a better script for this?
The last section, commented out, was something else I was trying, that didnt work.
The following code works in IE, but in firefox the tab,delete,backspace and arrow keys dont work.
Anyone got a better script for this?
Code:
function OnlyNumbers(e)
{
var keynum
var keychar
var numcheck
if(window.event) // IE
{
keynum = e.keyCode
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
numcheck = /\d/
return numcheck.test(keychar)
//if( (keynum == 190 || keynum == 109 || keynum == 8) ||
// (keynum >= 48 && keynum <= 57) ||
// (keynum == 46) ||
// (keynum >=37 && keynum <=40) ||
// (keynum == 9) ||
// (keynum >= 96 && keynum <= 105) ) { return true; }
// else { return false; }
}
The last section, commented out, was something else I was trying, that didnt work.