Click here to Skip to main content
13,150,946 members (45,110 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 18 May 2008

Check maxlength of multiline textbox in JavaScript

, 21 Feb 2012
Rate this:
Please Sign up or sign in to vote.
This post will help you to resolve problem of checking maxlength of Multiline textbox (Internet Explorer, Chrome & Firefox)


ASP.NET provides textbox controls for data inputs and they are used vastly on the websites. TextMode='Multiline' property of textbox is used everywhere to input comments or any large data.

With my program assignment, I came across a problem to set MaxLength for multiline textbox, i.e., after some specified length, the textbox should not accept any characters.

(Note: This solution was only tested & recommended for Internet Explorer (5+), Chrome & Firefox.)


The MaxLength property of textbox control works fine if Multiline mode is not set, but if TextMode='Multiline' is set in aspx page, then this property is of no use and user can input any number of characters inspite of setting maxlength.

Finally, I was able to find a solution and wanted to share it with you all.

This example requires just very basic knowledge of JavaScript, so don’t be scared. I promise this will be one of the easiest JavaScripts that can solve a complex problem.

Add the following to your Multiline box in aspx page:

<asp:TextBox Rows="5" Columns="80" ID="txtCommentsForSearch" 

onkeyDown="return checkTextAreaMaxLength(this,event,'10');" 

onblur="checkLength(this, '10')" TextMode="multiLine" 

CssClass="textArea" runat="server"> </asp:TextBox>        

*txtCommentsForSearch-This is the ASP.NET control that is having multiline property set.

I have used MaxLength='1999', same property you have to use in underlying JavaScript file also. I have also passed this length to the calling JavaScript method, so that in case the MaxLength is not accessible, then it can be picked from parameters of JavaScript method.

In new browser versions, MaxLength of multiline textbox is not accessible in JavaScript, so it's good to assign the required maxlength in JavaScript method calls.

Add the following to your JavaScript file:

// JScript File

function checkTextAreaMaxLength(textBox, e, length) {

            var mLen =  length;
            var maxLength = parseInt(mLen);
            if (!checkSpecialKeys(e)) {
                if (textBox.value.length > maxLength - 1) {
                    if (window.event)//IE
                        e.returnValue = false;
                        return false;

        function checkSpecialKeys(e) {
            if (e.keyCode != 9 && e.keyCode != 8 && e.keyCode != 46 && 
            e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && 
            e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
                return false;
                return true;
        function checkLength(textBox, length) {
            var mLen = length;
            var maxLength = parseInt(mLen);
            if (textBox.value.length > maxLength) {
                textBox.value = textBox.value.substring(0, maxLength);

Note: This implementation is written just to provide a basic and quick help in JavaScript. Nowdays Jquery is preferred and has more easy ways to do things. I myself have moved to Jquery since the past few years. I will post the similar article in Jquery too if anyone is interested.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Surjit Singh Dadhwal
Software Developer (Senior)
India India
Programming is my passion and C# is my friend, Javascript is my first crush..
Surprized!!!, don't be....
I love doing programming and writing efficient code, with the help of code project; I am able to share my experiences too with you all.

Thanks for visiting and voting this post.. [Smile | :) ]

You may also be interested in...

Comments and Discussions

QuestionPaste the text maximum than 10 characters is not working Pin
Umesh Deshmukh3510-Sep-14 1:04
memberUmesh Deshmukh3510-Sep-14 1:04 
AnswerRe: Paste the text maximum than 10 characters is not working Pin
Surjit Singh Dadhwal23-Sep-14 20:31
memberSurjit Singh Dadhwal23-Sep-14 20:31 
QuestionMARana Pin
axeemg23-Jan-14 20:37
memberaxeemg23-Jan-14 20:37 
AnswerRe: MARana Pin
Member 107868794-May-14 4:24
memberMember 107868794-May-14 4:24 
GeneralRe: MARana Pin
Surjit Singh Dadhwal23-Sep-14 20:31
memberSurjit Singh Dadhwal23-Sep-14 20:31 
GeneralMy vote of 3 Pin
kiran.puchakayala27-Jun-12 2:55
memberkiran.puchakayala27-Jun-12 2:55 
GeneralMy vote of 5 Pin
bengeorge10-Apr-11 10:19
memberbengeorge10-Apr-11 10:19 
GeneralDoes not work if user directly pastes the text from clip board Pin
arpitk15-Jan-09 0:04
memberarpitk15-Jan-09 0:04 
GeneralRe: Does not work if user directly pastes the text from clip board Pin
Paul Lockett23-Aug-10 1:16
memberPaul Lockett23-Aug-10 1:16 
Generalpasting Pin
Strider699-Sep-08 12:48
memberStrider699-Sep-08 12:48 
QuestionRe: pasting Pin
seth.dhawal21-Dec-08 19:39
memberseth.dhawal21-Dec-08 19:39 
Generalnot cross-browser solution Pin
ADLER119-May-08 10:28
memberADLER119-May-08 10:28 
GeneralRe: not cross-browser solution Pin
Surjit Singh Dadhwal22-May-08 19:50
memberSurjit Singh Dadhwal22-May-08 19:50 
GeneralNeed Proper Formatting before Approve Pin
Abhijit Jana18-May-08 22:33
memberAbhijit Jana18-May-08 22:33 
GeneralRe: Need Proper Formatting before Approve Pin
Surjit Singh Dadhwal22-May-08 19:51
memberSurjit Singh Dadhwal22-May-08 19:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 22 Feb 2012
Article Copyright 2008 by Surjit Singh Dadhwal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid