PDA

View Full Version : Numeric Textbox with format



tqm1
20 Dec 2011, 11:47 PM
Dear Experts

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>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">

<title>Numeric Textbox Sample</title>

<style type="text/css">
#box1 {width:200px;height:170px;border:1px solid green;background:#e3eeff;padding-top:20px;}

.button {width:100px;margin-top:0px;}

body {
margin:0;
margin-top:100px;
}
</style>

<script language="javascript">

function NumericDotOnly()
{
var key = window.event.keyCode;

if (((key >47) && (key <58)) || (key==46))
window.event.returnValue = true;
else
window.event.returnValue = false;
}

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="form2.text1.focus()" >
<center>
<p>I need following format<br>
99,999,999.99<br>
</p>
<div id="box1">
<form name=form2>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td>Amount</td>

<td><input type=text name=text1 size=15 value="" onkeypress="form2.text1.value=NumericDotOnly

()";onblur="form2.text1.value=formatCurrency(this.value)"> </td></tr>

<tr><td>Tax</td> <td><input type=text name=text2 size=15 value=""

onkeypress="form2.text2.value=NumericDotOnly()";onblur="form2.text2.value=formatCurrency(this.value)"></td></tr>
<tr><td>Result</td><td><input type=text name=text3 size=15 value="" disabled></td></tr>

</table>
<hr>
<input class="button" type=button name=command1 value="Plus"

onclick="form2.text3.value=parseFloat(form2.text1.value)+parseFloat(form2.text2.value)"><br>

<input class="button" type=button name=command8 value="Focus"

onclick="form2.text1.select()";"form2.text1.focus()"><br>
<input class="button" type=reset name=command9 value="Reset">

</form>
</div>

</center>
</body>
</html>


Please help me to apply following format

99,999,999.99

Thanks in advance