Ir para conteúdo

POWERED BY:

Arquivado

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

nanduuu

sobre Máscara

Recommended Posts

Ae pessoal,

Fiz essa máscara:

 

function check_fone(field){	var checkstr = "0123456789";	var FoneField = field;	var FoneValue = "";	var FoneTemp = "";	var seperator = '-';	var i;	err = 0;	FoneValue = FoneField.value;      /* Deleta todos os caracteres exceto de 0 a 9. */   for (i = 0; i < FoneValue.length; i++) {   if (checkstr.indexOf(FoneValue.substr(i,1)) >= 0) {      FoneTemp = FoneTemp + FoneValue.substr(i,1);   }   }FoneValue = FoneTemp;   if (FoneValue.length == 7) {//Se fone tiver 7 digitos      FoneValue = FoneValue.substr(0,3) + '-' + FoneValue.substr(3,4); }//coloca o "xxx-xxxx" if (FoneValue.length == 8) {//Se fone tiver 8 digitos      FoneValue = FoneValue.substr(0,4) + '-' + FoneValue.substr(4,4); }//coloca o "xxxx-xxxx" FoneField.value = FoneValue;}

Quando eu coloco 8 número não tem problema, mas quando eu coloco 7 número o número de telefone aparece com dois "--", por exemplo: 123--4567!

 

O que eu estou errando?

 

VALEU!

Compartilhar este post


Link para o post
Compartilhar em outros sites

colocou o html?

 

 

<html><body><form>   <tr><td>Fone:</td><td><input type="text" name="str_fone_cod" maxlength="2" size="2" onkeypress="return txtBoxFormat(document.myForm, 'str_fone_cod', '99', event);">-<input type="text" name="str_fone" maxlength="9" size="9" onkeypress="return txtBoxFormat(document.myForm, 'str_fone', '9999-9999', event);" onblur="check_fone(this)"></td></tr></form></body></html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Agora sim.. tem tudo! http://forum.imasters.com.br/public/style_emoticons/default/joia.gif

foi mal! http://forum.imasters.com.br/public/style_emoticons/default/blush.gif

 

function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {      var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;      var strCheck = '0123456789';//Caracteres válidos      var whichCode = (window.Event) ? evtKeyPress.which : evtKeyPress.keyCode;      var key = "";            if(document.all) {        nTecla = evtKeyPress.keyCode; }      else if(document.layers) {        nTecla = evtKeyPress.which;      }      sValue = objForm[strField].value;      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;	//Teste de teclas validas	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	        while (i <= mskLen) {        bolMask = ((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;}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Porque não posso usar outra função?

 

É que eu preciso que quando fulano comece a digitar a mascara já atue e faça a sua formatação... Por exemplo: tenho um numero 5555555 e quando eu digito o 5555... ele já fique 555-5... entendeu?

 

Mas como tem alguns numero de telefones com 7 casas e outros com 8 casas preciso que os com 7 casas fike 555-5555 e os com 8 5555-5555, por isso as duas funcoes...

 

Acho que eu expliquei mal, mas tem como eu fazer isso? e como?

 

VALEUUU! GRANDE AS AJUDAS! http://forum.imasters.com.br/public/style_emoticons/default/joia.gif http://forum.imasters.com.br/public/style_emoticons/default/clap.gif

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.