Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable textarea by height not characters

So many times we want to limit how much a user can write, but here I have a special sized box that it has to fit in, so I want to disable adding more characters if it would surpass a specific height. here is what I did:

var over;
$('textarea').keypress(function(e){
    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    var t = $(this).val();
    jQuery('<div/>', {
        style: "visibility:hidden",
        text: t,
        id: "test"
    }).appendTo('body');
    var h = $('#test').height();
    if(h >= 100){
        over = true;
    }
    else{
        over = false;
    }
    if(over){
        //code goes here
    }
    $('#test').remove();
});

I got the limiting code (what goes where I have the "code goes here" comment) from here and it ALMOST works.

There is only one problem:

if somebody copies and pastes, it can place multiple characters and therefore still go over the limit.

How can I fix this issue?

jsfiddle

like image 832
Ryan Saxe Avatar asked Aug 28 '13 15:08

Ryan Saxe


People also ask

How do I restrict textarea size?

In some cases, there is a need of putting a limit on the size of characters that can be typed in a textarea. So in that case, we can use maxlength attribute to control the number of characters entered in a textarea.

How do I stop textarea from resizing horizontally?

To prevent a text field from being resized, you can use the CSS resize property with its "none" value. After it you can use the height and width properties to define a fixed height and width for your <textarea> element.

Is disabled valid for textarea?

Disabled: Removed Entirely From the FormWhen you add the disabled to a textarea the form that the textarea is a part of will ignore the textarea input when the form is submitted. This is a good option to use if you want to remove a textarea from a form unless some other condition is met.


2 Answers

Another somewhat hacky solution could be to check scroll on key up. If scroll exists, delete the last character while scroll exists:

function restrictScroll(e){
    if(e.target.clientHeight<e.target.scrollHeight){
        while(e.target.clientHeight<e.target.scrollHeight){
            e.target.value = e.target.value.substring(0, e.target.value.length-1);
        }
    }
};
document.getElementById("textareaid").addEventListener('keyup', restrictScroll);

This would work as you type and if you paste blocks of text. Large text blocks may take a little longer to loop through though. In which case you may want to split on "\n" and remove lines first, then characters.

jsfiddle

like image 155
Eclectic Avatar answered Oct 02 '22 10:10

Eclectic


If you want your function to fire whenever the text in your field changes, you can bind it to the input and propertychange events, as per this answer:

  • https://stackoverflow.com/a/5494697/20578

Like this:

$('#descrip').on('input propertychange', function(e){

This will make sure your code fires when e.g. the user pastes in content using the mouse.

As for stopping them from entering content that would go over the limit, I think you have to keep track of what content they've entered yourself, and then revert their last edit if it infringed your criteria.

Note that e.g. Twitter doesn't stop the user from entering more characters if they've gone over the limit - they just tell the user they're over the limit, and tell them when they're back under. That might be the most usable design.

like image 41
Paul D. Waite Avatar answered Oct 02 '22 11:10

Paul D. Waite