Inquiridor
Máscara de Moeda a partir do 1.000,00

Pergunta
-
Bom dia, Pessoal.
Tudo bom?
Tenho um TextBox com máscara de moeda, está funcionando perfeitamente, através de uma função Javascript.
Porém, preciso agora alterar essa função e não sei como fazer. Do jeito que está, a máscara adiciona por exemplo, se eu coloco um número apenas ele formata assim: 0,01
Preciso agora que ao colocar um número, o meu textbox formate assim: 1.000,00
Se eu colocar 2 numeros, fique assim: 12.000,00
Segue minha função:
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); // Valor para o código da Chave if (strCheck.indexOf(key) == -1) return false; // Chave inválida len = objTextBox.value.length; for(i = 0; i < len; i++) if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break; 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 > 2) { aux2 = ''; for (j = 0, i = len - 3; i >= 0; i--) { if (j == 3) { aux2 += SeparadorMilesimo; j = 0; } aux2 += aux.charAt(i); j++; } objTextBox.value = ''; len2 = aux2.length; for (i = len2 - 1; i >= 0; i--) objTextBox.value += aux2.charAt(i); objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len); } return false; }
Espero que tenham entendido.
Muito obrigado, aguardo ajuda, por favor.
At.
Todas as Respostas
-
-
Cara não entendi muito bem o que vc necessita realmente, mas fiz aqui um exemplo e está funcionando.
Se der explica melhor ae..
Segue o exemplo.
<%
@ Page Language="C#" AutoEventWireup="true" CodeBehind="AltoCompletarJquery.aspx.cs"
Inherits="teste123.AltoCompletarJquery" %>
<%
@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!
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 language="javascript">
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; // Chave invlida
len = objTextBox.value.length;
for(i = 0; i < len; i++)
if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
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 > 2) {
aux2 =
'';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += SeparadorMilesimo;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
objTextBox.value =
'';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
objTextBox.value += aux2.charAt(i);
objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
}
return false;
}
</
script>
</
head>
<
body>
<form id="form1" runat="server">
<div>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<cc1:MaskedEditExtender ID="txt_" runat="server" Enabled="True" Mask="99/99/9999 99:99"
MaskType="Date" TargetControlID="txt" ClearMaskOnLostFocus="False">
</cc1:MaskedEditExtender>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txt"
Display="Dynamic" ErrorMessage="Informe a Data" Operator="GreaterThan" Type="Date"
Style="z-index: 0; left: 11px; position: absolute; top: 73px" ValueToCompare="01/01/1900"></asp:CompareValidator>
<asp:Button ID="btn" runat="server" Text="Button" OnClick="btn_Click" />
<br />
Valor moeda
<asp:TextBox ID="txtMoeda" runat="server" onKeyPress="return(MascaraMoeda(this,'.',',',event))">
</
asp:TextBox>
<br />
</div>
</form>
</
body>
</
html>
CASO A RESPOSTA TENHA AJUDADO FAVOR MARCAR COMO RESPONDIDA.
-
Então, amigo.
Você não alterou nada no script né?
O que eu preciso realmente é o seguinte, dessa forma que tá a mascara, ele formata assim os valores:
0,01 > 0,10 > 1,00, conforme o usuario digita, certo?
Eu quero, que ao digitar 1 no textbox, por exemplo, o valor fique: 1.000,00
Se o cara digitar 2 depois do 1, fique assim: 12.000,00
Entendeu?
-
Veja esta mascara em Dollar talvez você adaptando de certo:
http://www.pengoworks.com/workshop/js/mask/Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/ -
-
-
Veja este plugin em JQuery, eu testei e achei bem interessante:
Download:
https://github.com/plentz/jquery-maskmoney
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> <!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> <title></title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script> <script src="jquery.maskMoney.js" type="text/javascript"></script> <script> $(function () { $("#testeMascaraMoney").maskMoney(); }) </script> </head> <body> <form id="form1" runat="server"> <asp:TextBox runat="server" ID="testeMascaraMoney"></asp:TextBox> </form> </body> </html>
Veja este post também:
http://blog.caixadesolucoes.com.br/post/mascara-para-campos-monetarios-com-jquery-e-maskmoney
Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/- Editado Vitor Mendes segunda-feira, 12 de março de 2012 16:16
-
-
-
-
-
Cara ve se serve isso.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="FormatarCampos.aspx.cs" Inherits="ExemplosMSDN_FormatarCampos" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server"> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> Campo em milhar (1 = 1.000,00) <asp:TextBox ID="txtValorMilhar" runat="server" AutoPostBack="true" OnTextChanged="txtValorMilhar_TextChanged"></asp:TextBox> </ContentTemplate> </asp:UpdatePanel> </asp:Content>
cod.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class ExemplosMSDN_FormatarCampos : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void txtValorMilhar_TextChanged(object sender, EventArgs e) { txtValorMilhar.Text = String.Format("{0:N5}", Convert.ToInt32(txtValorMilhar.Text)).Replace("00000", "000,00"); } }
- Editado Romy Moura quarta-feira, 21 de março de 2012 20:23