JavaScripts: Prevent a User from Entering Non-numeric Characters
This article was contributed by Bart T. McDonough.Environment: IE 4.0 and above
Function Name: "CheckNumeric()"
To use this function, simply call it as an "onkeypress()" event handler in your <INPUT> tag in your HTML code. To see how this is done, view or download the source code below.
This function does not require any input parameters and does not output any return
value.
function CheckNumeric()
{
// Get ASCII value of key that user pressed
var key = window.event.keyCode;
// Was key that was pressed a numeric character (0-9)?
if ( key > 47 && key < 58 )
return; // if so, do nothing
else
window.event.returnValue = null; // otherwise,
// discard character
}
Downloads
Download demo project - .97 KbDownload source - 1.11 Kb
History
Date Posted: January 31, 2002# # #
0 Comments (click to add your comment)
