Format Value into Dollar using javascript

Posted by Vickypatel under JavaScript on 12/29/2014 | Points: 10 | Views : 498 | Status : [Member] | Replies : 1
Format decimal or int value to dollar format using javascript method.
For example, if used 10000 then using below method it will be replaced with 100,00.

       
<script type="text/javascript">
function formatDollar(num, obj) {
num = num.replace(/,/g, '');
if (isNaN(parseFloat(num)) == false) {
var p = parseFloat(num).toFixed(2).split(".");
obj.value = p[0].split("").reverse().reduce(function (acc, num, i, orig) {
return num + (i && !(i % 3) ? "," : "") + acc;
}, "") + "." + p[1];

}
}

function ValidateCurrency(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if ((charCode > 47 && charCode < 58) || (charCode == 8 && charCode == 46 && charCode == 16 && charCode == 27 && charCode == 37 && charCode == 39 && charCode == 38 && charCode == 40 && charCode == 36)) {
return true;
}
else {
return false;
}
}
</script>

How to use:

<asp:TextBox ID="txtEstimatedValue" onblur="javascript:formatDollar(this.value,this);" onkeyup="javascript:return ValidateCurrency(event)" runat="server" CssClass="textbox" MaxLength="12"></asp:TextBox>





Responses

Posted by: kgovindarao523-21772 on: 12/30/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

You don't have any issue right?you are just sharing code. Looking good. but its not the right section to place code snippets here.
Post in codes section, http://www.dotnetfunda.com/codes/

Thank you,
Govind

Vickypatel, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response