sometimes it is required to restrict the length of the textarea so that user should not enter characters beyond a certain limit.
For this we can make use of this simple javascript.
First we will create a 2 textarea.
Here we have created 2 textboxes "mytextarea" and "mytextarea1".
and onkeyup event of the mytextarea we will call our javascript function "restrict(textareaname)", means whenever any key is pressed we will call our javascript function.
and onchange event of the mytextarea we will call our javascript function "restrict(textareaname)", means whenever we will move out of this textbox javascript function will be called.
Here is our javascript function
function restrict(mytextarea) {
var maxlength = 15; // specify the maximum length
if (mytextarea.value.length > maxlength)
mytextarea.value = mytextarea.value.substring(0,maxlength);
} // -->
Include this function either in the head section or at the end before body tag.
Here is full source code to try this
Tweet this!