Frage

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?

War es hilfreich?

Lösung

You can use Javascript to implement maxlength in Internet Explorer.

<textarea rows="4" cols="50" maxlength="50" onKeyPress="return(this.value.length < 50);">

Andere Tipps

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;
    }
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top