Click here to Skip to main content
15,119,637 members
Articles / Web Development / ASP.NET
Technical Blog
Posted 17 Feb 2011


9 bookmarked

Why MaxLength property of a textbox with multiline mode doesn’t work? A Solution

Rate me:
Please Sign up or sign in to vote.
4.96/5 (10 votes)
17 Feb 2011CPOL2 min read
Why MaxLength property of a textbox with multiline mode doesn’t work? A Solution

Many developers set the MaxLength property of a Textbox and it works fine until the Textbox is not in multiline mode, which is by default behavior of a Textbox.

So what is the basic difference between a textbox in single line and multiline mode!!!

Let’s see, how it gets rendered as HTML. Let's have a look.

Textbox with SingleLine mode

Textbox with SingleLine mode

Textbox with MultiLine mode

Textbox with MultiLine mode

As you can see, when a Textbox is in SingleLine mode, it gets rendered as a input type text and when it is in MultiLine mode, it gets rendered as a textarea. As we know, MaxLength property works only for input type.

So what is the solution? We need to have a custom solution for this.

Let’s try some solution.

One thing we can do, we can attach a function on onkeypress event to the textbox, which first checks the length of input string and if exceeds with limit, it just returns else allow to enter any input. In the below example, I have used the maximum length as 10.

<asp:TextBox ID="tb" runat="server" TextMode="MultiLine" 
	onkeypress="return CheckLength();"></asp:TextBox>

//And the javascript code is
function CheckLength() {
            var textbox = document.getElementById("<%=tb.ClientID%>").value;
            if (textbox.trim().length >= 10) {
                return false;
            else {
                return true;

This works fine until a user doesn’t paste a text which is greater than the max length that is given (here 10). So there is no simple way to stop this. To overcome this problem, one should use one of the ASP.NET validators.

One can go for the custom validator, and provide a JavaScript function, which checks the length of the text and if exceeds the maxlength then show an error. Let’s see the code below:

<asp:TextBox ID="tb" runat="server" TextMode="MultiLine" ></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server" 
	ErrorMessage="Please enter maximum 10 characters." ControlToValidate="tb"
         SetFocusOnError="true" ClientValidationFunction="CheckMaxLength" >

//Client Validator function
function CheckMaxLength(sender, args) {
            if (args.Value.trim().length >

But there is another smart way to achieve this. We can also use Regular expression validator for this, which will check the count of the entered character and will throw an error if it exceeds. Here we would not require to write a JavaScript function. We need to have a regular expression that will work. Let’s see the code:

<asp:TextBox ID="tb" runat="server" TextMode="MultiLine" ></asp:TextBox>

//Regular Expression validator
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
	runat="server" ControlToValidate="tb" 
	ErrorMessage="Please enter maximum 10 charachters."
 SetFocusOnError="true" ValidationExpression="^[a-zA-Z.]{0,10}$">

Here we don’t require to write a JavaScript function. I have used a regular expression ^[a-zA-Z.]{0,10}$, which allows all the characters with length 0 to 10.

Hope you all must have enjoyed.




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


About the Author

Software Developer (Senior)
India India
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.

He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.

He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website

He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.

Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.

Visit his Blog: Code Wala

Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008

Comments and Discussions

GeneralMy vote of 5 Pin
Sk. Tajbir7-Mar-13 0:17
MemberSk. Tajbir7-Mar-13 0:17 
GeneralMy vote of 5 Pin
Kunal Chowdhury «IN»26-Feb-11 8:50
professionalKunal Chowdhury «IN»26-Feb-11 8:50 
GeneralMy vote of 5 Pin
lovejun20-Feb-11 18:48
Memberlovejun20-Feb-11 18:48 
GeneralRe: My vote of 5 Pin
Brij21-Feb-11 9:23
mentorBrij21-Feb-11 9:23 
GeneralRe: My vote of 5 Pin
lovejun21-Feb-11 18:57
Memberlovejun21-Feb-11 18:57 
GeneralMy vote of 5 Pin
RaviRanjanKr18-Feb-11 3:00
professionalRaviRanjanKr18-Feb-11 3:00 
GeneralRe: My vote of 5 Pin
Brij18-Feb-11 7:22
mentorBrij18-Feb-11 7:22 
GeneralMy vote of 5 Pin
Sudeep35017-Feb-11 20:21
MemberSudeep35017-Feb-11 20:21 
GeneralRe: My vote of 5 Pin
Brij18-Feb-11 7:23
mentorBrij18-Feb-11 7:23 

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.