Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

67
Vistas
Limit line breaks in textarea

I have a text area, where i want to allow users to make line break by pressing enter, but i want to limit that to only one break, because then the users could add as many breaks they want, creating huge white space in the output.

  <textarea maxLength={3000} onChange={(e) => setPersonBio(e.target.value)} value={personBio} type="text" rows="5" className="bio-content" placeholder="Enter something about yourself..."></textarea>

And then i display it in <p> tag with white-space: pre-line;

My question is how to limit the amount of breaks user can enter, or delete the additional ones after submitting.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use the String methods search and include inside a while loop to remove all but one of a given repeated character.

text = "abc\n\n\ndef\n\nghi"; 
document.write('<textarea rows=6>'+text+'</textarea>');
while (text.includes("\n\n"))
  text = text.replace("\n\n","\n");
document.write('<textarea rows=6>'+text+'</textarea>');

about 4 years ago · Santiago Trujillo Denunciar

0

You could try this:

 <textarea maxLength={3000} onChange=textAreaChange(e) value={personBio} type="text" rows="5" className="bio-content" placeholder="Enter something about yourself..."></textarea>
var pre="";
function textAreaChange(e){
    if(e.target.value.split("\n").length>1){
        e.target.value=pre;
        return;
    };
    setPersonBio(e.target.value);
    pre=e.target.value;
};

Add the javascript to where you keep all your other javascript for this site.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda