How to make a Textbox numeric using JavaScript
posted June 1, 2008 - 6:39amAs we all know that using JavaScript, we can capture the keycode of the key. So I have used this logic to validate the user input. When user will enter any key in textbox at that time its keycode will checked in javascript function below.
*************************************************************
JavaScript Function < b>
function fInputNumericValuesOnly()
{
if(!(event.keyCode==45||event.keyCode==46||event.keyCode==48||event.keyCode==49||event.keyCode==50||event.keyCode==51||event.keyCode==52||event.keyCode==53||event.keyCode==54||event.keyCode==55||event.keyCode==56||event.keyCode==57))
{
event.returnValue = false;
}
}
Explanation
I have validated the keycode from 0 to 9. If the keycode is not from 0 to 9 then it will return the false value. So user can not enter the character except 0 to 9.
You have to just call this function in onKeyDown part of the textbox property.

Comments
I Beg to Differ
Follow my posts: taranitely
Yes you can do so but actual
SC
A Better Way
if (event.keyCode < 45 || event.keyCode > 57)...just a thought.Follow my posts: taranitely
Post new comment