Máscara nos campos

2 respostas
E

Olá pessoal!
Estou desenvolvendo uma aplicação e não sei como fazer campos com máscaras. Utilizo o framework Struts e já tentei algumas coisas, tipo:
<html:text property=“zipCode” size=“9” maxlength=“9” onkeydown=“if(this.value.length==5{this.value=this.value + ‘-’});”/>
Mas não funcionou.
Alguem tem alguma sugestão??

Obrigado! :slight_smile:

2 Respostas

D

rapá..
vamo lá..

fiz um exemplinho aqui funcionando blz!
Dei o import no meu arquivo js
e na linha do cep fiz

CEP&#58; html&#58;text styleClass=&quot;edtTexto&quot; name=&quot;enderecoClienteForm&quot; property=&quot;cep&quot; size=&quot;10&quot; maxlength=&quot;10&quot; onkeydown=&quot;FormataCep&#40;this,8,event&#41;&quot;

Ps.: Tive que eliminar tag's html pois o phpnuke não aceita

Blz?
Ai chamo a função Formata CEP no arquivo Js
A função é esta:

function FormataCep&#40;campo,tammax,teclapres&#41; &#123;
	if &#40;!ehNumericoPlus&#40;teclapres&#41;&#41; &#123;
	   teclapres.returnValue = false;
	   return;
	&#125;

	var tecla = teclapres.keyCode;
	vr = campo.value;
	vr = vr.replace&#40; &quot;-&quot;, &quot;&quot; &#41;;
	vr = vr.replace&#40; &quot;.&quot;, &quot;&quot; &#41;;
	tam = vr.length;

	if &#40;tam &lt; tammax &amp;&amp; tecla != 8&#41;&#123;
	  tam = vr.length + 1 ;

 	  if &#40;tecla == 8 &#41;&#123;	tam = tam - 1 ; &#125;

	  if &#40; tecla == 8 || tecla &gt;= 48 &amp;&amp; tecla &lt;= 57 || tecla &gt;= 96 &amp;&amp; tecla &lt;= 105 &#41;&#123;
		if &#40; tam &lt;= 3 &#41;&#123;
	 		campo.value = vr ; &#125;
	 	if &#40; &#40;tam &gt; 3&#41; &amp;&amp; &#40;tam &lt;= 6&#41; &#41;&#123;
	 		campo.value = vr.substr&#40; 0, tam - 3 &#41; + '-' + vr.substr&#40; tam - 3, tam &#41; ; &#125;
	 	if &#40; &#40;tam &gt;= 7&#41; &amp;&amp; &#40;tam &lt;= 8&#41; &#41;&#123;
	 		campo.value = vr.substr&#40; 0, tam - 6 &#41; + '.' + vr.substr&#40; tam - 6, 3 &#41; + '-' + vr.substr&#40; tam - 3, tam &#41; ; &#125;
	  &#125;
	&#125;
&#125;

no mais é só..
se não funcionar grita ae

E

Opa Diogo!
Funcionou quase direito! :slight_smile:
Seguinte… voce poderia me passar essa função ehNumericoPlus()?
Outra coisa… o Cep está vindo num formato assim: 37.540-00
tá faltando 1 zero… tipo 37.540-000
Mudei algumas coisas no código para q conseguisse fazer isso, mas não consegui.
Mas mesmo assim, muito obrigado! :slight_smile:

Criado 14 de outubro de 2004
Ultima resposta 14 de out. de 2004
Respostas 2
Participantes 2