0
votes

How to make a Textbox numeric using JavaScript

posted June 1, 2008 - 6:39am
How to make a Textbox numeric using JavaScript

As 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.


Tags:

Comments

I Beg to Differ

Do you realise that there is no difference between the results of either of those pieces of code? They both return true and false for the same ranges.

Follow my posts: taranitely

Yes you can do so but actual

Yes you can do so but actual will be if (event.keyCode >= 45 && event.keyCode <= 57)

SC

A Better Way

Instead of typing all that out, why didn't you just say... if (event.keyCode < 45 || event.keyCode > 57) ...just a thought.

Follow my posts: taranitely

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Lines and paragraphs break automatically.
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • Allowed HTML tags: <p> <br> <b> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <span> <object> <param> <embed> <table> <tr> <td> <div>
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Join Xomba Today

Do you like to write? Would you like to make a little extra money on the side? These people do. Join the Xomba community today.
Become a Member