0

I currently try to restrict the the maximal amount of characters allowed in a textarea.

With:

<textarea rows="4" cols="50" maxlength="50">

It works like it should in Firefox, however there seems to be no effect in IE which poses a problem since quite a lot of the website-users still use IE.

Do you have any suggestions or a workaround?

thwd
  • 23,956
  • 8
  • 74
  • 108
user2425234
  • 67
  • 1
  • 10

2 Answers2

2

You can use Javascript to implement maxlength in Internet Explorer.

<textarea rows="4" cols="50" maxlength="50" onKeyPress="return(this.value.length < 50);">
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

I am suggesting this since you had placed php in the tags, you can truncate the input from the server side using substr

$trunc = substr($_POST['textareaname'], 0, 50);

alternatively you can also use Javascript function.

UPDATE to your comment on how to provide a feedback to the user when limit is reached.

$("#element").keypress(function (e) {
    var str = $(this).val();
    if (str.length > 100) {
        e.preventDefault();
        alert('You have reached max limit');
        return false;
    }
});
Community
  • 1
  • 1
DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • Is there a way to let the user know that he has reached the max-amount of chars? Because I basically want to stop the user from inputting more than 100 chars, in the code itself I already do the substr – user2425234 Jul 11 '13 at 09:23
  • yes you can, very simply if you use jquery. do you use jquery? – DevZer0 Jul 11 '13 at 09:24