Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to limit the amount of text that can be entered into a 'textarea'?

What is the best way to limit the amount of text that a user can enter into a 'textarea' field on a web page? The application in question is ASP .NET, but a platform agnostic answer is preferred.

I understand that some amount of javascript is likely needed to get this done as I do not wish to actually perform the 'post' with that amount of data if possible as ASP .NET does have an upper limit to the size of the request that it will service (though I don't know what that is exactly).

So maybe the real question is, what's the best way to do this in javascript that will meet the following criteria:

-Must work equally well for both users simply typing data and copy/paste'ing data in from another source.

-Must be as '508 compliance' friendly as possible.

like image 449
Jesse Taber Avatar asked Nov 29 '22 21:11

Jesse Taber


2 Answers

function limit(element, max_chars)
{
    if(element.value.length > max_chars)
        element.value = element.value.substr(0, max_chars);
}

As javascript, and...

<textarea onkeyup="javascript:limit(this, 80)"></textarea>

As XHTML. Replace 80 with your desired limit. This is how I do it anyway.

Note that this will prevent the user from typing past the limit in the textbox, however the user could still bypass this using javascript of their own. To make sure, you must also check with your server side language.

like image 136
Logan Serman Avatar answered Dec 08 '22 00:12

Logan Serman


use a RegularExpressionValidator Control in ASP.Net to validate number of character along with with usual validation

like image 25
yesraaj Avatar answered Dec 07 '22 22:12

yesraaj