Jump to content
Fórum Script Brasil
  • 0

Problema com função de formatar moeda


Andryon PHProgrammer

Question

Olá pessoal,

estou utilzando a seguinte função para adicionar a máscara de moeda ao campo:

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 || whichCode == 8) 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;
}
<tr bgcolor="#FFFFCA" >
                            <td id="hora1" width="68%"><b>Horas extras</b></td>
                            <td id="hora2" width="14%"></td>
                            <td id="hora3" width="16%"></td>
                            <td id="hora4"><input type="text" name="HoraExtra"id="HoraExtra" value=""  onKeyPress="return(MascaraMoeda(this,'.',',',event))" style="width:100px"  /></td>
                        </tr>
                         <tr bgcolor="#FFFFCA" >
                            <td><b>Adicional noturno</b></td>
                            <td></td>
                            <td></td>
                            <td><input type="text" name="AdNoturno" id="AdNoturno" value=""  onKeyPress="return(MascaraMoeda(this,'.',',',event))" style="width:100px"  /></td>
                        </tr>

mas sempre que clico no adicional noturno para colocar o valor, pisca a seleção do campo e volta para o campo de hora extra, alguém sabe se tem algum conflito, bug ou se estou fazendo alguma coisa errada?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...