Validating only Numbers in javascript
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   SaiPrabhu12_PrabhuSai21
Posted On:   Wednesday, May 19, 2004 01:11 AM

I want the textbox to accept only numbers, not any characxters or special characters. how to do it.

Re: Validating only Numbers in javascript

Posted By:   Anonymous  
Posted On:   Monday, June 7, 2004 08:39 AM

What you have to do is:

putin the "onkeypress" property this JS:

onkeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"

thay way the textbox only will accept numbers.
There is only one bad thing about that JS, it only works with Internet Explorer because other browsers work differntly with events.

Re: Validating only Numbers in javascript

Posted By:   Sridevi_Lakshminarayanan  
Posted On:   Wednesday, May 19, 2004 01:57 PM

Hi,

Call the following function in the onkeypress event of the text box .






//For Numeric only checking
function fnIsNumeric(event)
{
var KeyTyped = String.fromCharCode(event.keyCode);

if(parseInt(KeyTyped.toString()) != KeyTyped.toString())
{
return false;
}
else
{
return true;
}
}


Thank you , Sri

Re: Validating only Numbers in javascript

Posted By:   Sean_Owen  
Posted On:   Wednesday, May 19, 2004 11:41 AM

If you are using Struts, look at the Validator component. You can easily configure it to validate that a field is numeric -- either on the server side, or on the client side using Javascript.

Re: Validating only Numbers in javascript

Posted By:   Firat_Tiryaki  
Posted On:   Wednesday, May 19, 2004 01:26 AM

use the isNaN (is not a number) function... returns true if the parameter included is not a number



alert(isNaN("a")); -> alerts true

alert(isNaN(10)); -> alerts false

About | Sitemap | Contact