Friday, 2 November 2012

Currecy format in textbox using java script


here the code for that....
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Currencyformat.aspx.cs" Inherits="Currencyformat" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
function MaskMoney1(evt, allow) {

            if (!(evt.keyCode == 46 || (evt.keyCode >= 48 && evt.keyCode <= 57))) return false;
            var parts = evt.srcElement.value.split('.');
            if (parts.length > 2) return false;
            if (evt.keyCode == 46) return (parts.length == 1);
            if (parts[0].length >= 14) return false;
            if (parts.length == 2 && parts[1].length >= allow) return false;
        }


    function insertcommas(nField) {
        if (/^0/.test(nField.value)) {
            nField.value = nField.value.substring(0, 1);
        }
        if (Number(nField.value.replace(/,/g, ""))) {
            var tmp = nField.value.replace(/,/g, "");
            tmp = tmp.toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,').split('').reverse().join('').replace(/^,/, '');
            if (/\./g.test(tmp)) {
                tmp = tmp.split(".");
                tmp[1] = tmp[1].replace(/\,/g, "").replace(/ /, "");
                nField.value = tmp[0] + "." + tmp[1]
            }
            else {
                nField.value = tmp.replace(/ /, "");
            }
        }
        else {
            nField.value = nField.value.replace(/[^\d\,\.]/g, "").replace(/ /, "");
        }

    }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>Currency Format using Javascript</p>
    <asp:TextBox runat="server" ID="TextBox4" onKeyPress="return MaskMoney1(event,2)" onkeyup="insertcommas(this)"></asp:TextBox>
   <asp:TextBox runat="server" ID="TextBox3" onKeyPress="return MaskMoney1(event,2)" onkeyup="insertcommas(this)"></asp:TextBox>
    <asp:TextBox runat="server" ID="TextBox2" onKeyPress="return MaskMoney1(event,2)" onkeyup="insertcommas(this)"></asp:TextBox>
    <asp:TextBox runat="server" ID="TextBox1" onKeyPress="return MaskMoney1(event,2)" onkeyup="insertcommas(this)"></asp:TextBox>
    </div>
   
    </form>
</body>
</html>

Happy Coding

1 comment:

  1. How do instead comma view dot
    1,234,567
    convert
    1.234.567

    ReplyDelete