Javascript Currency Money not working correctly

I have a javascript function that duplicate any number i press, for example:
I type: 1
It writes: 0,011 (instead of only 0,01)

The function works fine if i enable the "return false" in the end of the function, but the problem is that enabling it, it causes some functionality problems on my page.

 The javascript function used The textbox used
LVL 3
tbsolutionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
Maybe off topic but check this plugin : http://digitalbush.com/projects/masked-input-plugin/

test page :
<%@ Page Title="Page d'accueil" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
<script language="javascript" type="text/javascript" src="http://cloud.github.com/downloads/digitalBush/jquery.maskedinput/jquery.maskedinput-1.3.min.js"></script>
<script language="javascript" type="text/javascript">

    $(document).ready(function () {
        $("#<%= txtValorStrike.ClientID %>").mask("9.999.999,99");
    });

</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="txtValorStrike" CssClass="TextBoxCommon" runat="server" Width="134px" AutoPostBack="True" OnTextChanged="txtValorStrike_TextChanged"></asp:TextBox>
</asp:Content>

Open in new window

tbsolutionsAuthor Commented:
Good solution leakim971, but the company doesn't accept jQuery plugins yet....
Thanks!
leakim971PluritechnicianCommented:
You can do the postback yourself :  
onblur="setTimeout('__doPostBack(\'<%= txtValorStrike.ClientID %>\',\'\')', 0)"

Open in new window



<%@ Page Title="Page d'accueil" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<!--
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
<script language="javascript" type="text/javascript" src="http://cloud.github.com/downloads/digitalBush/jquery.maskedinput/jquery.maskedinput-1.3.min.js"></script>
-->
<script language="javascript" type="text/javascript">

/*
    $(document).ready(function () {
        $("#<%= txtValorStrike.ClientID %>").mask("9.999.999,99");
    });
*/
    window.onload = function () {
        alert("page (re)loaded or postback occured!");
    }

    function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e) {
        var sep = 0;
        var key = '';
        var i = j = 0;
        var len = len2 = 0;
        var strCheck = '0123456789';
        var aux = aux2 = '';
        var whichCode = (window.event) ? e.which : e.keyCode;
        if (whichCode == 13) return true;
        key = String.fromCharCode(whichCode);
        if (strCheck.indexOf(key) == -1) return false;
        len = objTextBox.value.length;
        for (i = 0; i < len; i++)
            if (strCheck.indexOf(objTextBox.value.charAt(i)) != -1) aux += objTextBox.value.charAt(i);
        aux = '';
        for (; i < len; i++)
            if (strCheck.indexOf(objTextBox.value.charAt(i)) != -1) aux += objTextBox.value.charAt(i);
        aux += key;
        len = aux.length;
        if (len == 0) objTextBox.value = '';
        if (len == 1) objTextBox.value = '0' + SeparadorDecimal + '0' + aux; ;
        if (len == 2) objTextBox.value = '0' + SeparadorDecimal + aux;
        if (len == 3) objTextBox.value = '0' + SeparadorDecimal + aux;
        if (len == 4) objTextBox.value = '0' + SeparadorDecimal + aux;
        if (len > 4) {
            aux = '';
            for (j = 0, i = len - 5; i >= 0; i--) {
                if (j == 3) {
                    aux2 += SeparadorMilesimo;
                    j = 0;
                }
                aux2 += aux.charAt(i);
                j++;
            }
            objTextBox.value = '';
            len2 = aux.length;
            for (i = len2 - 2; i >= 0; i--) {
                objTextBox.value += aux2.charAt(i);
            }
            objTextBox.value += SeparadorDecimal + aux.substr(len - 4, len);
        }
        return false;
    }

</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="txtValorStrike" onkeypress="return MascaraMoeda(this,'.',',',event)" onblur="setTimeout('__doPostBack(\'<%= txtValorStrike.ClientID %>\',\'\')', 0)" CssClass="TextBoxCommon" runat="server" Width="134px" AutoPostBack="True" OnTextChanged="txtValorStrike_TextChanged"></asp:TextBox>
</asp:Content>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tbsolutionsAuthor Commented:
You got it! I was about to give up...
Thank you very much!!!
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points! Have a nice week!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.