Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/07/2005, 01:02
Avatar de KarlanKas
KarlanKas
Moderador extraterrestre
 
Fecha de Ingreso: diciembre-2001
Ubicación: Madrid
Mensajes: 6.987
Antigüedad: 23 años, 2 meses
Puntos: 61

Código PHP:
<script>
function 
comprobar(esto){
texto=esto.value;
if(
texto.charAt(0)!="("){texto="("+texto;}
if(
texto.length>3){
if(
texto.charAt(4)!=")"){
resto=texto.substring(4);
texto=texto.substring(0,4)+")"+resto;
}}
esto.value=texto;

}
</script>
<input type="text" onkeyup="comprobar(this)"> 
Aunque así es menos agresivo:

Código PHP:
<script>
function 
comprobar(esto){

texto=esto.value;
if(
texto.length==1){
if(
texto.charAt(0)!="("){texto="("+texto;}
else{
texto=""}
}
if(
texto.length==5){
if(
texto.charAt(4)!=")"){
resto=texto.substring(4);
texto=texto.substring(0,4)+")"+resto;
}
else{
texto=texto.substring(0,4)}
}
esto.value=texto;

}
</script>
<input type="text" onkeyup="comprobar(this)"> 
Digo que es menos agresivo porque espera a haber pasado el caracter para poner el paréntesis... y además, consigues que si se confunde el usuario no tiene que borrar los paréntesis. Se borran solos.
__________________
Cómo escribir

No hay pregunta tonta, sino tonto que quiere seguir en la ignorancia.

Última edición por KarlanKas; 15/07/2005 a las 01:15