Jump to content


Photo

Mascaramoeda


  • Faça o login para participar
7 replies to this topic

#1 Stormbringer

Stormbringer

    I'd love to stay with you all

  • Ex-Admins
  • 2927 posts
  • Sexo:Não informado
  • Localidade:Goiânia - GO
  • Interesses:Atualmente: pesquisa e desenvolvimento de web-games

Posted 20/05/2010, 17:09

tenho a sequinte funçao

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var j = 0;
    var i = 0;
    var len2 = 0;	
    var len = 0; 
    var strCheck = '0123456789';
    var aux2 = '';
    var aux = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
	//alert(whichCode);
    if (whichCode == 13 || whichCode == 8 || whichCode == 9 || whichCode == 0) 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;
}


ela funciona belezinha... POREM, tentei altera-la pra funcionar com apenas 1 casa decimal, e me atrapalhei... alguem tem uma funçao similar que trabalha com apenas 1 casa decimal?





exemplo de uso


<input value="" name="nota" style="text-align:right; onKeyPress="return(MascaraMoeda(this,'','.',event))" onKeyDown="if(this.value.length > 4){this.value = this.value.substring(1,5)}" onKeyUp="if(this.value.length > 4){this.value = this.value.substring(1,5)}" type="text" size="2" >



abraços

Edição feita por: Stormbringer, 20/05/2010, 17:09.

๑۩۞۩๑Let the Carnage Begin!!๑۩۞۩๑


#2 Cristiano Galdino

Cristiano Galdino

    Dark Defender

  • Administradores
  • 3738 posts
  • Sexo:Masculino
  • Localidade:Brasília, DF

Posted 20/05/2010, 17:48

Storm! Blzinha?! Dá uma olhada aqui:

http://jsfromhell.co...format-currency
Cristiano Galdino
- http://cristiano.galdino.net/

“Since 2003”


#3 Stormbringer

Stormbringer

    I'd love to stay with you all

  • Ex-Admins
  • 2927 posts
  • Sexo:Não informado
  • Localidade:Goiânia - GO
  • Interesses:Atualmente: pesquisa e desenvolvimento de web-games

Posted 20/05/2010, 18:02

opa cristiano...

nem rola

Error: addEvent is not defined
Source File: http://www.webwar.co...elpro/teste.php
Line: 18

addEvent(o, "keypress", function(e, _){




mesmo assim valeu, to tentando ainda alterar a funçao original, ehhehe

Edição feita por: Stormbringer, 20/05/2010, 18:04.

๑۩۞۩๑Let the Carnage Begin!!๑۩۞۩๑


#4 Cristiano Galdino

Cristiano Galdino

    Dark Defender

  • Administradores
  • 3738 posts
  • Sexo:Masculino
  • Localidade:Brasília, DF

Posted 20/05/2010, 18:05

Olha as Dependências. ;)
Cristiano Galdino
- http://cristiano.galdino.net/

“Since 2003”


#5 Stormbringer

Stormbringer

    I'd love to stay with you all

  • Ex-Admins
  • 2927 posts
  • Sexo:Não informado
  • Localidade:Goiânia - GO
  • Interesses:Atualmente: pesquisa e desenvolvimento de web-games

Posted 21/05/2010, 09:57

nope... nao rolou
http://www.webwar.co...elpro/teste.php

vou deixar isso por ultimo no projeto :D

oooopa segui um caminho diferente, indicado pelo meu amigo Zebass... rolou:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script>

//sonumeros
function validaTECLA(campo,e){
var strCheck = '0123456789';
var whichCode = (window.Event) ? e.which : e.keyCode;
	//alert(whichCode);
    if (whichCode == 13 || whichCode == 8 || whichCode == 9 || whichCode == 0) return true;
	key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
	
//return true;	
}

// Formata o campo valor
function formataNota(campo) {
	campo.value = filtraCampo(campo);
	VR = campo.value;
		vr = tirarZerosEsquerda(VR);
	tam = vr.length;

	if ( (tam > 1) && (tam <= 4) ){
 		campo.value = vr.substr( 0, tam - 1 ) + '.' + vr.substr( tam - 1, tam ) ; }
 	

 		
}


// limpa todos os caracteres especiais do campo solicitado
function filtraCampo(campo){
	var s = "";
	var cp = "";
	vr = campo.value;
	tam = vr.length;
	for (i = 0; i < tam ; i++) {  
		if (vr.substring(i,i + 1) != "/" && vr.substring(i,i + 1) != "-" && vr.substring(i,i + 1) != "."  && vr.substring(i,i + 1) != "," ){
		 	s = s + vr.substring(i,i + 1);}
	}
	campo.value = s;
	return cp = campo.value
}

function tirarZerosEsquerda(STR){
	var sAux = '';
	STR = new String(STR);  
	var i = 0;
	while (i < STR.length ){
		if ((STR.charAt(i)!='.') && (STR.charAt(i)!=',')){
			sAux += STR.charAt(i);
		}
		i++
	}
  STR = new String(sAux);
  sAux = '';
  i = 0;
  while (i < STR.length ){
    if (STR.charAt(i) != '0'){
      sAux = STR.substring(i,STR.length)
	  i = STR.length;
	}
    i++;
  }
  return  sAux;
}

</script>
<body>
<form name="form" autocomplete="off">

<input name="valor" type="text" class="t02" id="valor" dir="rtl" onKeyPress="return validaTECLA(this, event)" onKeyUp="formataNota(this);if(this.value.length > 4){this.value = this.value.substring(1,5)}" size="22" maxlength="22" />

</form>
</body>
<script>
document.form.valor.focus();

</script>
</html>


valeu

edit again... nao rolou no ie, entao o validaTECLA assim ficou melho0r:

function validaTECLA(campo,event){
	var BCK=8,ETR=13,TAB=9,key,tecla;CheckTAB=true;
	if(event.which)tecla=event.which;
	else tecla=event.keyCode;
	key=String.fromCharCode(tecla);
//	if(tecla==ETR)return true;
	if(tecla==BCK)return true;
	if(tecla==TAB)return true;
	return(/[0-9]/.test(key));
}

Edição feita por: Stormbringer, 21/05/2010, 09:50.

๑۩۞۩๑Let the Carnage Begin!!๑۩۞۩๑


#6 marvinnanet

marvinnanet

    Novato no fórum

  • Usuários
  • 2 posts
  • Sexo:Masculino
  • Localidade:pelotas, rio grande do sul

Posted 14/06/2010, 22:03

Ola pessoal, gostaria de uma ajuda
estou com fazendo um campo monetário, mas preciso multiplicar o valor pro outro. Só que peguei vi um script que achei legal de um colega do forum, que é esse
em js:

/**
* Função para aplicar máscara em campos de texto
* Copyright © 2008, Dirceu Bimonti Ivo - http://www.bimonti.net
* All rights reserved.
* @constructor
*/

/* Version 0.27 */

/**
* Função Principal
* @param w - O elemento que será aplicado (normalmente this).
* @param e - O evento para capturar a tecla e cancelar o backspace.
* @param m - A máscara a ser aplicada.
* @param r - Se a máscara deve ser aplicada da direita para a esquerda. Veja Exemplos.
* @param a -
* @returns null
*/
function maskIt(w,e,m,r,a){

// Cancela se o evento for Backspace
if (!e) var e = window.event
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;

// Variáveis da função
var txt = (!r) ? w.value.replace(/[^\d]+/gi,'') : w.value.replace(/[^\d]+/gi,'').reverse();
var mask = (!r) ? m : m.reverse();
var pre = (a ) ? a.pre : "";
var pos = (a ) ? a.pos : "";
var ret = "";

if(code == 9 || code == 8 || txt.length == mask.replace(/[^#]+/g,'').length) return false;

// Loop na máscara para aplicar os caracteres
for(var x=0,y=0, z=mask.length;x<z && y<txt.length;){
if(mask.charAt(x)!='#'){
ret += mask.charAt(x); x++;
} else{
ret += txt.charAt(y); y++; x++;
}
}

// Retorno da função
ret = (!r) ? ret : ret.reverse()
w.value = pre+ret+pos;
}

// Novo método para o objeto 'String'
String.prototype.reverse = function(){
return this.split('').reverse().join('');

Dae eu tenho a multiplicação que é

<script>
function multiplicacao(){
subtotal = document.multi.sub.value;
ip = 0.03200;
document.multi.total.value = subtotal*ip;
}
function SomenteNumero()
{
if (event.keyCode<48 || event.keyCode>57)
{
return false;
}
}
</script>

e as respostas:
<form name="multi">
<div align="center">
<input type="text" name="sub" OnKeyPress="return SomenteNumero()" onkeyup="maskIt(this,event,'###.###.###,##',true,{pre:'R$',pos:''})">
<br>
<br>
<input type="" name="ipi" value="coeficiente" OnKeyPress="return SomenteNumero()" disabled="disabled">
<br>
<span class="style7">Valor da parcela em 36x</span><br>
<input type="text" name="total" OnKeyPress="return SomenteNumero()">
<br>
<br>
<input type="button" value="calcular" onClick="multiplicacao()">
<input name="Reset" type="reset" onClick="multiplicacao()" value="apagar">
<br>



Só que quando vai multiplicar os campos, não multiplica, aparece umas letras.

Por que isso ocorre? agradeço desde já a ajuda

#7 André Manoel

André Manoel

    Doutor

  • Usuários
  • 996 posts
  • Sexo:Masculino
  • Localidade:Brasilia

Posted 15/06/2010, 09:27

TEnte colocar isso aqui :

subtotal = parseFloat(document.multi.sub.value);

(y)
Iniciando na Ajuda On line...

Posted Image Meu post lhe ajudou? Reputar/votar é uma das formas de agradecer.

#8 unidimension

unidimension

    Novato no fórum

  • Usuários
  • 3 posts
  • Sexo:Masculino
  • Localidade:Brasília

Posted 17/10/2010, 17:47

Dê uma olhada aqui:

Clique aqui




1 user(s) are reading this topic

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq