0

Dear Sir,

I have following codes

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<title>Numeric Textbox</title>
<SCRIPT LANGUAGE="JAVASCRIPT">
//------------------------------------------------------------------
function isNumberKey(evt)
    {
   var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 31 && (charCode < 48     || charCode > 57 || charCode == 110))
{
alert("Enter Numeric data Only");      
return false;
}
else
{
   return true;
}
}

//------------------------------------------------------------------
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}

//------------------------------------------------------------------

</script>
</head>

<body onload="form1.text1.focus()">
<center>
<fieldset style=width:300px;background-color:#E0FFFF )>
<Legend style=color:blue;><b><font size=5>Numeric Input</font></b></legend>

<form name="form1">
<table cellpadding="1" cellspacing="3" border="0">
<tr>
<td>1st Number</td>
<td><input   type="text" name="text1" onkeypress="return isNumberKey(event)"; 

onblur="form1.text1.value=formatCurrency(form1.text1.value)" >
</td>
</tr>

</table>
</form>
</fieldset>
</center>
</body>
</html>

The textbox accept only numeric values.
How to modify codes to accept . (decimal).

I want to enter data as

254228.25

Please help

Edited by tqmd1

3
Contributors
2
Replies
25
Views
3 Years
Discussion Span
Last Post by JorgeM
0

Your if statement on line 12 looks a little off. Try this one, I haven't tested:

if( charCode < 48 || (charCode > 57 && charCode != 110 && charCode != 190) ) { ... }

keycode 110 = . on Num Pad
keycode 190 = a period (.)

1

Try this conditional statement...

fac8bed50b118e2559d37c251529686e

Also, change the event from onkeypress to onkeyup.

onkeyup="return isNumberKey(event)";

sorry for the screen pic instead of the text code, this editor would not allow me to paste the code.

Edited by JorgeM

Votes + Comments
Nice catch, I meant to include num pad numbers and ended up forgetting
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.