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

Recommended Answers

All 2 Replies

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 (.)

Try this conditional statement...

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.

commented: Nice catch, I meant to include num pad numbers and ended up forgetting +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.