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

143
Vistas
Javascript or Jquery textarea counter with minimum text length?

I can't find any examples when you want the user to input at least x number of characters in a textarea. There is no max length, only minimum. Is there any simple solutions to this?

This is what I have now.

A simple textarea

<textarea id="textComment" name="comment" ></textarea>

Div area showing x characters remaining to write

<div>Characters left <span id="charsLeft"></span></div>

If possible, disable post button until x characters reached.

<button onclick="frmComment.submit();return false;">Add comment</button>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <textarea name="" id="textComment" cols="30" rows="10"></textarea>
    <script>
      var maxln = 5; //write your max length
      document.getElementById("textComment").addEventListener("keyup", () => {
        if (document.getElementById("textComment").value.length >= maxln) {
          document.getElementById("textComment").value = document
            .getElementById("textComment")
            .value.substring(0, maxln); // this removes any character after max value
        }
      });
    </script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is done with jq:

$(document).ready(function(){

//Set this to whatever size you want 
var characterSize = ;
$("#charCount").text(characterSize);

$("#textComment").keyup(function() {
    
  count = $("#textComment").val().length;
  
  if(count < characterSize){
  $("#charCount").text(characterSize - count);
  }
  else
  {
    $("#charCount").text(0);
  }

  if ($("#textComment").val().length >= characterSize){
  $("button").attr('onclick', 'frmComment.submit()', 'return false');
  }

  else {
    $("button").removeAttr('onclick', null);
   }
     
});
});

Change the HTML to this:

    <textarea id="textComment" name="comment" ></textarea>

    <div>Characters left: <a id="charCount"></a> <span id="charsLeft"></span></div>

    <button >Add comment</button>

When the user inserts more than X characters jq will kick in and give the button the onclick property. Before reaching the character limit the button will do nothing. And if the user writes more than X characters and then erases some and the number falls below X the button will once again do nothing.

I imagine it could be simplified but this way you can understand how it works.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can change the minimum length on textarea element => data-minLength="10"

$(document).ready(function() {
 $("#textComment").on("keyup", function(){
     var minLength = $(this).attr("data-minlength");
     var currentLength = $(this).val().length;
     var remaining = parseInt(minLength) - parseInt(currentLength)
     $("#charsLeft").text((remaining < 0 ? 0: remaining));
     
     if (parseInt(currentLength) < parseInt(minLength))
     {
         $("#commentBtn").prop("disabled", true);
     } else{
         $("#commentBtn").prop("disabled", false);
     }
     
     
 });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="textComment" data-minLength="10" name="comment" ></textarea>
<div>Characters left: <span id="charsLeft"></span></div>
<button id="commentBtn" disabled onclick="frmComment.submit();return false;">Add comment</button>

about 4 years ago · Juan Pablo Isaza 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