• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1446
  • Last Modified:

Javascript Currency Mask for Asp.Net

My page has a textbox that uses a javascript currency mask function, the mask works fine. TextBox AutoPostBack property is enabled, but the only key that calls the "PostBack" to start the "onTextChanged" event is the "Enter".  I need to call the "PostBack/onTextChanged" when the field loose it's focus (not only pressing "Enter" but for example with "Tab" or simply clicking out). I tried to remove the mask, and everything worked. I don't know if the problem is the script function or the page behavior.
 The textbox used The currency function used
0
tbsolutions
Asked:
tbsolutions
  • 5
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Remove the return false at the end of the MascaraMoeda function
0
 
tbsolutionsAuthor Commented:
The textbox keeps responding only when you press "Enter".
0
 
leakim971PluritechnicianCommented:
Sorry, remove the return in onkeypress too
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
leakim971PluritechnicianCommented:
Like this :


<asp:TextBox ID="txtValorStrike" onkeypress="MascaraMoeda(this,'.',',',event)" CssClass="TextBoxCommon" runat="server" Width="134px" AutoPostBack="True" OnTextChanged="txtValorStrike_TextChanged"></asp:TextBox>

Open in new window

0
 
leakim971PluritechnicianCommented:
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">

    window.onload = function () {
        alert("alert 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="MascaraMoeda(this,'.',',',event)" CssClass="TextBoxCommon" runat="server" Width="134px" AutoPostBack="True" OnTextChanged="txtValorStrike_TextChanged"></asp:TextBox>
</asp:Content>

Open in new window

0
 
tbsolutionsAuthor Commented:
That's it! Thanks.
0
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now