Ver Mensaje Individual
  #5 (permalink)  
Antiguo 12/02/2013, 10:52
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 17 años, 8 meses
Puntos: 1567
Respuesta: calcular fin de linea en textarea

Cita:
Iniciado por brnb8626 Ver Mensaje
al parecer no me entendieron

tengo el siguiente codigo
Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
<script type="text/javascript">

function limitaLinea(id){
	var obj=document.getElementById(id);
	var vlr=obj.value.split('');
	var cnt=1;
	var rows=obj.rows;
	var key = window.event.keyCode || window.event.which;
	for(i=0;i<vlr.length;i++)
		if(vlr[i]=="\n" || vlr[i]=="\r")
			cnt++;
	if(cnt>=rows && (key==10 || key==13))
		return false;
	else
		return true;
}
</script>
</head>
<body>
<textarea id="area" onkeypress="return limitaLinea(id)" rows="3" cols="1" style="width:250px"></textarea>
</body>
</html> 
la funcion recibe el id del texarea, obtenemos su contenido y la propiedad rows
el contenido se guarda en un arreglo cuando el usuario presiona una tecla se recorre el arreglo y cuenta los \n o \r
si el contador(cnt) llega a ser = o mayor que rows retorna un false y evitamos que se siga escribiendo

para los que no saben:
\n = nueva linea
\r = retorno

pero esta funcion no funciona cuando el usuario sigue escribiendo y nunca presiona la tecla enter
lo que busco es calcular el fin de la linea para agregar un \n y evitar que siga escribiendo despues de los rows establecidos en el texarea

espero aberme explicado mejor
no se muy bien que intentás, pero empecemos por lo sencillo

onkeypress="return limitaLinea(id)"
no deberias pasar el valor del id?
onkeypress="return limitaLinea('area', event)"
y que la función reciba (id,evtn)

Con lo siguiente lo que hace es que despues de escribir 3 lineas te congele el ingeso de más caracteres

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Documento sin título</title>
  5. <script type="text/javascript">
  6.  
  7. function limitaLinea(id,evnt){
  8.     var obj=document.getElementById(id);
  9.     var vlr=obj.value.split('');
  10.     var cnt=1;
  11.     var rows=obj.rows;
  12.     var ev = (evnt) ? evnt : event;
  13.    var key=(ev.which) ? ev.which : event.keyCode;
  14.    
  15.     for(i=0;i<vlr.length;i++)
  16.         if(vlr[i]=="\n" || vlr[i]=="\r")
  17.             cnt++;
  18.     if(cnt>=rows && (key==10 || key==13))
  19.         return false;
  20.     else
  21.         return true;
  22. }
  23. </head>
  24. <textarea id="area" onkeypress="return limitaLinea('area',event)" rows="3" cols="1" style="width:250px"></textarea>
  25. </body>
  26. </html>

SAludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.