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

前端 未结 6 572
生来不讨喜
生来不讨喜 2021-01-19 05:50

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 agnos

6条回答
  •  北海茫月
    2021-01-19 06:20

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

    As javascript, and...

    
    

    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.

提交回复
热议问题