Foros del Web » Programando para Internet » Javascript »

Trim

Estas en el tema de Trim en el foro de Javascript en Foros del Web. Hay algo parecido a la funcion trim en js???? quiero quitar los espacios vacios antes y despues de un string ayudenmeeeeee...
  #1 (permalink)  
Antiguo 28/11/2002, 20:51
Avatar de deprabado  
Fecha de Ingreso: febrero-2002
Mensajes: 649
Antigüedad: 22 años, 2 meses
Puntos: 0
Trim

Hay algo parecido a la funcion trim en js????
quiero quitar los espacios vacios antes y despues de un string ayudenmeeeeee
  #2 (permalink)  
Antiguo 29/11/2002, 00:04
Avatar de Mickel  
Fecha de Ingreso: mayo-2002
Ubicación: Lima, Peru
Mensajes: 4.619
Antigüedad: 22 años
Puntos: 7
function JSTrim(txtBox) {
while(''+txtBox.value.charAt(0)==' ') txtBox.value=txtBox.value.substring(1,txtBox.value .length);

while('' + txtBox.value.charAt(txtBox.value.length-1)==' ')txtBox.value=txtBox.value.substring(0,txtBox.val ue.length-1);
}
.....
<input type='text'... onBlur='JSTrim(this);'>
  #3 (permalink)  
Antiguo 12/06/2003, 15:07
 
Fecha de Ingreso: junio-2003
Ubicación: Buenos Aires Capital
Mensajes: 29
Antigüedad: 20 años, 10 meses
Puntos: 0
yo necesito lo mismo , pero a mi no me anduvo, a vos te anduvo???
  #4 (permalink)  
Antiguo 12/06/2003, 15:54
Avatar de Kaopectate
Colaborador
 
Fecha de Ingreso: diciembre-2001
Ubicación: Curaçao (Antillas Holandesas)
Mensajes: 3.179
Antigüedad: 22 años, 4 meses
Puntos: 38
Bueno, dado que la respuesta original es de noviembre de 2002, no voy a esperar a ver si Mickel la completa. De cualquier modo lo que estoy haciendo es completar un poco el código de él.

Código PHP:
<html>
 <
head>
   <
script language="JavaScript">

    function 
lTrim(sStr){
     while (
sStr.charAt(0) == " "
      
sStr sStr.substr(1sStr.length 1);
     return 
sStr;
    }

    function 
rTrim(sStr){
     while (
sStr.charAt(sStr.length 1) == " "
      
sStr sStr.substr(0sStr.length 1);
     return 
sStr;
    }

    function 
allTrim(sStr){
     return 
rTrim(lTrim(sStr));
    }

    function 
prueba(){
     
with (document.frm){
      
original.value ">" usuario.value "<";
      
lTrimmed.value ">" lTrim(usuario.value) + "<";
      
rTrimmed.value ">" rTrim(usuario.value) + "<";
      
allTrimmed.value ">" allTrim(usuario.value) + "<";
     }
    }

   
</script>
  </head>
 <body>
  <form name="frm">
   <table border="0">
    <tr>
     <td align="right">
      Valor
     </td>
     <td>
      <input type="text" name="usuario" value="    Un valor      "><br>
     </td>
    </tr>
    <tr>
     <td align="right">
      Original
     </td>
     <td>
      <input type="text" name="original" readonly><br>
     </td>
    </tr>
    <tr>
     <td align="right">
      Trim izquierdo
     </td>
     <td>
      <input type="text" name="lTrimmed" readonly><br>
     </td>
    </tr>
    <tr>
     <td align="right">
      Trim derecho
     </td>
     <td>
      <input type="text" name="rTrimmed" readonly><br>
     </td>
    </tr>
    <tr>
     <td align="right">
      Trim completo
     </td>
     <td>
      <input type="text" name="allTrimmed" readonly><br>
     </td>
    </tr>
    <tr>
     <td colspan="2" align="center">
      <input type="button" value="Calcula" onclick="prueba()">
     </td>
    </tr>
   </table>
  </form>
 </body>
</html> 
Saludos.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:54.