Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Borel

Formatar campo

Recommended Posts

Excelente tópico !

 

Eu estou aprendendo javascript ainda, sou estudante de Java, e estou desenvolvendo um projeto para estudos.

 

Estou com a mesma dificuldade dos colegas ae, no campo HORA, estou tentando ver se acho um jeito de corrigir isso !

 

Por acaso alguem ja conseguiu resolver o problem ? posta ae http://forum.imasters.com.br/public/style_emoticons/default/joia.gif/>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Estou com a mesma dificuldade dos colegas ae, no campo HORA, estou tentando ver se acho um jeito de corrigir isso ! Por acaso alguem ja conseguiu resolver o problem ?

<{POST_SNAPBACK}>

Cara, não faço a menor idéia! Funciona sem os segundos (00:00)!!! http://forum.imasters.com.br/public/style_emoticons/default/sick.gif/>

<{POST_SNAPBACK}>

ahn é ? que bom cara, resolve meu problema sem os segundo !!!

 

mas mesmo assim nao consegui fazer funcionar...

 

o que eu mudo ???

Compartilhar este post


Link para o post
Compartilhar em outros sites

<input type="text" name="str_hora" maxlength="5" size="5" onkeypress="return txtBoxFormat(document.Form, 'str_hora', '99:99', event);" onKeyUp="return autoTab(this, 5, event);">
As funções estão na primeira página deste tópico!

 

;)/>

<{POST_SNAPBACK}>

Eu mudei justamente isso, mas nada acontece http://forum.imasters.com.br/public/style_emoticons/default/sick.gif/>

Compartilhar este post


Link para o post
Compartilhar em outros sites

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

 

<script language="JavaScript" type="text/JavaScript">

 

function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {

var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

if(document.all) { // Internet Explorer

nTecla = evtKeyPress.keyCode; }

else if(document.layers) { // Nestcape

nTecla = evtKeyPress.which;

}

 

sValue = objForm[strField].value;

// Limpa todos os caracteres de formatação que

// já estiverem no campo.

sValue = sValue.toString().replace( "-", "" );

sValue = sValue.toString().replace( "-", "" );

sValue = sValue.toString().replace( ".", "" );

sValue = sValue.toString().replace( ".", "" );

sValue = sValue.toString().replace( "/", "" );

sValue = sValue.toString().replace( "/", "" );

sValue = sValue.toString().replace( "(", "" );

sValue = sValue.toString().replace( "(", "" );

sValue = sValue.toString().replace( ")", "" );

sValue = sValue.toString().replace( ")", "" );

sValue = sValue.toString().replace( " ", "" );

sValue = sValue.toString().replace( " ", "" );

fldLen = sValue.length;

mskLen = sMask.length;

 

i = 0;

nCount = 0;

sCod = "";

mskLen = fldLen;

 

while (i <= mskLen) {

bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ":") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))

bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

 

if (bolMask) {

sCod += sMask.charAt(i);

mskLen++; }

else {

sCod += sValue.charAt(nCount);

nCount++;

}

i++;

}

 

objForm[strField].value = sCod;

if (nTecla != 8) { // backspace

if (sMask.charAt(i-1) == "9") { // apenas números...

return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9

else { // qualquer caracter...

return true;

}

} else {

return true;

}

}

 

//Fim da Função Máscaras Gerais

 

/***

* AUTO TAB - ao prencher o campo, automaticamente manda o foco para o próximo!

***/

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {

var keyCode = (isNN) ? e.which : e.keyCode;

var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

if(input.value.length >= len && !containsElement(filter,keyCode)) {

input.value = input.value.slice(0, len);

input.form[(getIndex(input)+1) % input.form.length].focus();

}

 

function containsElement(arr, ele) {

var found = false, index = 0;

while(!found && index < arr.length)

if(arr[index] == ele)

found = true;

else

index++;

return found;

}

 

function getIndex(input) {

var index = -1, i = 0, found = false;

while (i < input.form.length && index == -1)

if (input.form == input)index = i;

else i++;

return index;

}

return true;

}

//Fim da Função AutoTab

</script>

</head>

 

<body>

Uso no Form:

 

CODE

<form name="Form">

<table border="0" cellspacing="2" cellpadding="2">

 

<tr>

<td>Hora</td>

<td><input type="text" name="str_hora" maxlength="5" size="5" onkeypress="return txtBoxFormat(document.Form, 'str_hora', '99:99', event);" onKeyUp="return autoTab(this, 5, event);"></td>

</tr>

 

</form>

 

</body>

</html>

ta assim

Compartilhar este post


Link para o post
Compartilhar em outros sites

Olá, Gostei muito da formatação pra 2 casas decimais. Tentei usar o script numa pagina php e não funcionou. O que tenho de fazer?

<{POST_SNAPBACK}>

A princípio nada... Posta aí como você fez...

<{POST_SNAPBACK}>

Segue o código....talvez tenha esquecido alguma declaração do js ?

 

Obrigado !! http://forum.imasters.com.br/public/style_emoticons/default/joia.gif/>

 

<html>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link href="css/css.css" rel="stylesheet" type="text/css">

<script language="JavaScript" type="text/JavaScript">

<!--

function MM_reloadPage(init) { //reloads the window if Nav4 resized

if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {

document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}

else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();

}

MM_reloadPage(true);

//-->

function FormataValor(campo,tammax,teclapres) {

var tecla = teclapres.keyCode;

vr = document.form[campo].value;

vr = vr.replace( "/", "" );

vr = vr.replace( "/", "" );

vr = vr.replace( ",", "" );

vr = vr.replace( ".", "" );

vr = vr.replace( ".", "" );

vr = vr.replace( ".", "" );

vr = vr.replace( ".", "" );

tam = vr.length;

if (tam < tammax && tecla != 8){ tam = vr.length + 1; }

if (tecla == 8 ){ tam = tam - 1; }

if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){

if ( tam <= 2 ){

document.form[campo].value = vr; }

if ( (tam > 2) && (tam <= 5) ){

document.form[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }

if ( (tam >= 6) && (tam <= 8) ){

document.form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }

if ( (tam >= 9) && (tam <= 11) ){

document.form[campo].value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }

if ( (tam >= 12) && (tam <= 14) ){

document.form[campo].value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }

if ( (tam >= 15) && (tam <= 17) ){

document.form[campo].value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );}

}

for (var ct = 0; ct < document.form.elements.length; ct++) {

if (document.form.elements[ct].name == document.form.elements[campo].name) {

if ( !teclapres.shiftKey && tecla == 9 && document.form.elements[ct+1] && document.form.elements[ct+1].name == "senhaConta" && document.applets['tclJava'] ){

document.applets['tclJava'].setFocus();

}

}

}

}

 

</script>

</head>

 

<body>

<table width="100%" border="0" cellpadding="2" cellspacing="2" bordercolor="#FF6600" bgcolor="#FF6600">

<tr>

<td bgcolor="#FF6600"><span class="formtit">ENCONTRE SEU IMÓVEL </span></td>

</tr>

</table>

<table width="100%" border="0" cellspacing="2" cellpadding="2">

<tr>

<td align="left" valign="top"><form name="form1" action="resultados_busca.php">

<select name="tipo" class="formbusca" id="select2">

<option value="casa" selected>Casa</option>

<option value="apartamento">Apartamento</option>

<option value="comercial">Comercial</option>

<option value="chacara">Chácaras</option>

<option value="terrenos">Terrenos</option>

</select>

<br>

<select name="transacao" class="formbusca" id="select3">

<option value="aluguel" selected>Aluguel</option>

<option value="venda">Venda</option>

</select>

<br>

<p>

<input name="valor" type="Text" id="valor" onKeyDown="FormataValor('valor', 13, event)" size="23" maxlength="17">

</p>

<input name="buscar" type="submit" class="formenvia" id="buscar" value="Buscar">

</form></td>

</tr>

</table>

</body>

</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

E viva o google ... http://forum.imasters.com.br/public/style_emoticons/default/joia.gif/>

 

 

Pra galera que tava com problemas no campo hora ... achei isso, e acho resolveu meu problema, espero que resolva o de vcs..

 

espero q ajude, AAlves, valew pelas dicas e ajuda !

 

edit: Vi um problema agora... se digitar RAPIDAMENTE os numeros nao aparece os : (dois pontos) :huh:/>

 

<script>function mascara() {if(document.form.data.value.length == 2) {document.form.data.value += ':';}if(document.form.data.value.length == 5) {document.form.data.value += ':';}}</script><form name=form><input type=text name=data onKeyUp="mascara()" maxlength=8></form>

Compartilhar este post


Link para o post
Compartilhar em outros sites

olá,

 

Ótimo script mas não entendo de javascript, gostaria de uma ajuda de vocês, como modifico o script para aceitar números sem virgula ex. 123 quando digitar o 4 irá aparecer 1.234?

 

Ele está limitado a 13 caracteres se eu colocar um número negativo irá contar o sinal de (-) como um caracter? como aumentar?

 

Gostaria de restringuir a digitação de alfa como fazer mas preciso apenas do sinal negativo

 

obrigado pela atenção. http://forum.imasters.com.br/public/style_emoticons/default/joia.gif/>

Compartilhar este post


Link para o post
Compartilhar em outros sites

como modifico o script para aceitar números sem virgula ex. 123 quando digitar o 4 irá aparecer 1.234?

<{POST_SNAPBACK}>

Exemplo:

 

<script language="JavaScript">function FormataValor(campo,tammax,teclapres) {var tecla = teclapres.keyCode;vr = document.form[campo].value;vr = vr.replace( "/", "" );vr = vr.replace( "/", "" );vr = vr.replace( ",", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );tam = vr.length;if (tam < tammax && tecla != 8){ tam = vr.length + 1; }if (tecla == 8 ){ tam = tam - 1; }if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){if ( tam <= 3 ){document.form[campo].value = vr; }if ((tam > 3) && (tam <= 6)){document.form[campo].value = vr.substr( 0, tam - 3 ) + '.' + vr.substr( tam - 3, tam ); }if ( (tam >= 7) && (tam <= 9) ){document.form[campo].value = vr.substr( 0, tam - 6 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, tam );}if ( (tam >= 10) && (tam <= 12) ){document.form[campo].value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, tam ); }}}</script>
Uso:

 

<form method="POST" name="form"><p><input type="Text" name="valor" sizAe="23" maxlength="15" onKeyDown="FormataValor('valor', 13, event)"></p></form>
A máscara vai até 999.999.999.999!

 

B)/>

<{POST_SNAPBACK}>

olá AAlves. Tudo bem?

 

eu tentei usar esse code, mas.... não funcionou. Dá um erro.

 

Linha: 49

Caractere: 1

Erro: Erro de sintaxe

 

Já verifiquei, fucei, deixei tudo do jeito que tava. E não arrumou.

creio eu, que, a linha 49 que "ele" fala é essa:

Banda: <br><br>Preço: <br><INPUT TYPE='text' NAME='preco' onKeyDown='FormataValor('preco', 13, event)'><br><br>Imagem da Capa: <br></FORM>

 

poderias me ajudar?

 

agradeço muito.

 

Pedro Márcio

Compartilhar este post


Link para o post
Compartilhar em outros sites

olá AAlves. Tudo bem? eu tentei usar esse code, mas.... não funcionou. Dá um erro.poderias me ajudar?

<{POST_SNAPBACK}>

Oi Pedro! Veja como está o nome do seu formulário! Esse exemplo usa um nome fixo: form!

 

B)/>

<{POST_SNAPBACK}>

<FORM METHOD=POST ACTION="<? $PHP_SELF ?>" enctype='multipart/form-data' name="form">

Compartilhar este post


Link para o post
Compartilhar em outros sites

<FORM METHOD=POST ACTION="<? $PHP_SELF ?>" enctype='multipart/form-data' name="form">

<{POST_SNAPBACK}>

E aí ? Funcionou ? Testei novamente e roda numa boa... :o/>

<{POST_SNAPBACK}>

Tipo, tem o name=form.. tah tudo certo... mas.....

 

NÃO FUNCIONA!

 

=(

 

tah assim:

 

<script language="JavaScript">function FormataValor(campo,tammax,teclapres) {var tecla = teclapres.keyCode;vr = document.form[campo].value;vr = vr.replace( "/", "" );vr = vr.replace( "/", "" );vr = vr.replace( ",", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );tam = vr.length;if (tam < tammax && tecla != 8){ tam = vr.length + 1; }if (tecla == 8 ){ tam = tam - 1; }if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){if ( tam <= 3 ){document.form[campo].value = vr; }if ((tam > 3) && (tam <= 6)){document.form[campo].value = vr.substr( 0, tam - 3 ) + '.' + vr.substr( tam - 3, tam ); }if ( (tam >= 7) && (tam <= 9) ){document.form[campo].value = vr.substr( 0, tam - 6 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, tam );}if ( (tam >= 10) && (tam <= 12) ){document.form[campo].value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, tam ); }}}</script>
se quiser o code completo, avisa!

 

valeuu!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Galera, vamos tentar fazer as máscaras funcionarem perfeitamente no Firefox. Quem tiver mais experiência com JavaScript vai ajudando aí.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Pessoal... lindo esse código abaixo:

 

<html><head><title>teste</title><script language="JavaScript">function FormataValor(campo,tammax,teclapres) {var tecla = teclapres.keyCode;vr = document.form[campo].value;vr = vr.replace( "/", "" );vr = vr.replace( "/", "" );vr = vr.replace( ",", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );tam = vr.length;if (tam < tammax && tecla != 8){ tam = vr.length + 1; }if (tecla == 8 ){ tam = tam - 1; }if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){if ( tam <= 2 ){document.form[campo].value = vr; }if ( (tam > 2) && (tam <= 5) ){document.form[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 6) && (tam <= 8) ){document.form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 9) && (tam <= 11) ){document.form[campo].value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 12) && (tam <= 14) ){document.form[campo].value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 15) && (tam <= 17) ){document.form[campo].value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );}}}</script></head><body><form method="POST" name="form"><p><input type="Text" name="valor" size="23" maxlength="17" onKeyDown="FormataValor('valor', 13, event)"></p></form></body></html>

 

hehehe

Mas COOOOOMOOOOO faço funcionar no Opera? =(

Ninguém aih testou no Opera? My God!

 

Alguém me ajuda? =(

Compartilhar este post


Link para o post
Compartilhar em outros sites

Seguinte cara, eu testei no Mozilla e ele não deixa nem eu digitar nada e nem dá erro.

<{POST_SNAPBACK}>

Pois é... para funcionar no Mozilla, altere isso:

 

if(document.all) { // Internet ExplorernTecla = evtKeyPress.keyCode; }else if(document.layers) { // NestcapenTecla = evtKeyPress.which;}
Por isso:

 

var nTecla = evtKeyPress.keyCode ? evtKeyPress.keyCode : evtKeyPress.which ? evtKeyPress.which : evtKeyPress.charCode;
Mas ainda há problema no backspace... Teste aí para você ver. Ele apaga até o primeiro caracter da máscara e depois morre! Não consegui resolver essa... http://forum.imasters.com.br/public/style_emoticons/default/cry.gif/>

 

http://forum.imasters.com.br/public/style_emoticons/default/excl.gif/>

<{POST_SNAPBACK}>

 

aqui???

Ué.. isso nem existe na função de moeda..

 

hehehe tou perdida!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Se chamar a função "apenas" no: onKeyUp, não irá funcionar corretamente...

Deve-se utilizar em: onKeyUp, onKeyDown ou OnKeyPress...

 

A validação não funcionou aqui não: Slack 10.2, Firefox 1.5!

Abraço!!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Pessoal, sei que já não é hora de voltar lá atrás, mas testei o código pra colocar valores separados por ponto a cada 3 casas e a vírgula nas casas decimais e deu tudo certinho. só que tenho uma dúvida: como ficaria o cógigo para aparecer com o R$ antes dos números? E se possível, o R$ alinhado à esquerda da caixa e os valores à direita. Tem como?Agradeço a todos.Marco

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.