1

I have this JQuery code to limit text in a textarea to 250 characters:

    $('#postform textarea').keypress(function() {
         if($(this).val().length >= 250) {
            $(this).val($(this).val().slice(0, 250));
            return false;
        }
    });

Is it possible to change this code so users can type past the limit in the textarea, then when the user clicks the submit button it shows an alert if it is over the limit.

Any help appreciated.

5 Answers5

1

Instead of keypress event use sumbit event on your form :

$('#form').submit(function(event) {
    var $textarea = $(this).find('textarea')
    if($textarea.val().length >= 250) {
        // do what you want
        // show an error or
        // E.g : highlight the textarea in red
        $textarea.addClass('input-error');
        return false;
    }
    else {
        $textarea.removeClass('input-error');
    }
});
.input-error {
  border-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form" action="" method="form">
  <textarea></textarea>
  <button type="submit">Submit</button>
  </form>
Louis Barranqueiro
  • 10,058
  • 6
  • 42
  • 52
1

Try to use maxlength attribute.

<textarea rows="4" cols="50" maxlength="10" placeholder='You can type just 10 characters'>
</textarea>

NOTE : The maxlength attribute of the textarea tag is not supported in Internet Explorer 9 and earlier versions, or in Opera 12 and earlier versions.

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
0

Yes you can. You just need to do a check on submission of the textarea and see that it isn't over the limit.

$('#my-form').submit(function() {
    if($('input[name="myTextArea"]').length >= 250) {
     alert("Hey your text is over 250");
    }
})
ngoctranfire
  • 793
  • 9
  • 15
0

You can actually have a message displaying characters left and whenever the user exceeds the limit and clicks on submit an alert box is displayed.

$('#postform textarea').keypress(function() {
         var c_left = lettersTyped($(this));
         if((c_left-20) < 0){
           $('p').html(20-c_left+' characters left').removeClass('error');
         }
         else{
           $('p').html('Limit exceeded!').addClass('error');
           }
    });
    function lettersTyped(text){
      var len = text.val().length;        
      return len;
    }

       $('#postform .submit').on('click', function(){
         var text = $('#postform textarea');
         if(lettersTyped(text) > 20){
          alert('You have exceeded the limit');
         }
         else{
          //not submitting the form here, but you can keep it true. 
          return false;
         }
       });
p{
color: lightgreen;
}
.error{
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<form id="postform">
 <textarea placeholder="Start typing.."></textarea>
 <p></p>
 <input type="submit" class="submit"/>
</form>
Sahil
  • 3,338
  • 1
  • 21
  • 43
0
$('#postform').submit(function() {
     if($('#postform textarea').val().length >= 250) {
        //message
        return false;
     }
     return true;
});
Oxi
  • 2,918
  • 17
  • 28