Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent user to enter text in textarea after reaching max character limit

Tags:

jquery

I want to prevent user to enter text in textarea once it reaches a max character limit. What was happening that when i reached to max limit then my text-area scroll-bar moved to top I somehow prevent this with this code.

jQuery(document).ready(function($) {     $('textarea.max').keyup(function() {         var $textarea = $(this);         var max = 400;         if ($textarea.val().length > max) {             var top = $textarea.scrollTop();             $textarea.val($textarea.val().substr(0, max));             $textarea.scrollTop(top);          }     }); }); //end if ready(fn) 

But i also want that after reaching max limit user unable to type anything in the textarea. Currently what happen that after reaching max limit if user press and hold the key, the characters are typing in the text area, although after releasing the button it come back to original text (i.e $textarea.val($textarea.val().substr(0, max)); ). But i want that once this condition become true

if ($textarea.val().length > max) { 

user unable to type anything. I want that cursor vanishes from the textarea. But if user remove some text then cursor also available for user to type input again. How can I do it?

like image 782
Basit Avatar asked May 02 '12 13:05

Basit


People also ask

How do I restrict characters in textarea?

Note − By default, we can enter data in a textarea upto 5,24,288 characters. 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.

What is the way to keep users from typing text into a large text area?

For input type="text" , we can use the size attribute to specify the visible size of the field, in characters. But we can also use the maxlength attribute to specify the maximum amount of characters that can be entered. Browsers generally enforce such a limit.

What is the maximum limit of textarea?

HTML <Textarea>maxlength attribute number: It contains single value number which allows the maximum number of character in Textarea element. Its default value is 524288.

Which attribute specifies the maximum number of characters allowed in the textarea?

The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> . This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the input or textarea has no maximum length.


2 Answers

The keyup event fires after the default behaviour (populating text area) has occurred.

It's better to use the keypress event, and filter non-printable characters.

Demo: http://jsfiddle.net/3uhNP/1/ (with max length 4)

jQuery(document).ready(function($) {     var max = 400;     $('textarea.max').keypress(function(e) {         if (e.which < 0x20) {             // e.which < 0x20, then it's not a printable character             // e.which === 0 - Not a character             return;     // Do nothing         }         if (this.value.length == max) {             e.preventDefault();         } else if (this.value.length > max) {             // Maximum exceeded             this.value = this.value.substring(0, max);         }     }); }); //end if ready(fn) 
like image 127
Rob W Avatar answered Oct 09 '22 07:10

Rob W


<textarea maxlength="400"> </textarea> 

Use the above code to limit the number of characters inserted in a text area, if you want to disable the textarea itself (ie, will not be able to edit afterwards) you can use javascript/jquery to disable it.

like image 21
KBN Avatar answered Oct 09 '22 06:10

KBN