Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 39160 |  Welcome, Guest!   Register  Login
 Home > Blogs > ASP.NET > Why MaxLength property of a textbox with multiline mode doesn't work? A Solution ...
Brij

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

 Blog author: Brij | Posted on: 2/27/2011 | Category: ASP.NET Blogs | Views: 6195 | Status: [Member] [Microsoft_MVP] | Points: 75 | Alert Moderator   
Ads

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 diffference between a textbox in single line and multiline mode!!!
Let's see, how it gets rendered as html. Lets have a look.



Now 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 check the length of input string and if exceed 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>
//Validator
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please enter maximum 10 charachters." ControlToValidate="tb"
SetFocusOnError="true" ClientValidationFunction="CheckMaxLength" ></asp:CustomValidator>
//Client Validator function
function CheckMaxLength(sender, args) {
if (args.Value.trim().length >= 10) {
args.IsValid = false;
}
}

But there is another smart way is there, to acheive 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}$"></asp:RegularExpressionValidator>
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.

 Thanks,
 Brij



Cheers,
Brij
Microsoft MVP - ASP.NET/IIS
http://brijbhushan.net
Found interesting? Add this to:



 More Blogs from Brij

     More ...

    About Brij Mishra

    Experience:0 year(s)
    Home page:http://brijbhushan.net
    Member since:Friday, April 09, 2010
    Level:Starter
    Status: [Member] [Microsoft_MVP]
    Biography:Brij is a Microsoft MVP in ASP.NET/IIS. He is a very passionate dotnet programmer.He is fond learning latest microsoft technologies and love to share the knowledge to others.

    Visit his blog: http://brijbhushan.net
    Follow him on Twitter: @brij_bhushan

     Responses

    SheoNarayan
    Posted by: SheoNarayan | Posted on: 2/28/2011 | Level: HonoraryPlatinum | Status: [Microsoft_MVP] [Administrator] | Points: 15 | Alert Moderator 

    Very well thought & logical Brij, keep submitting more blog posts.

    Regards



    Regards,
    Sheo Narayan, Microsoft MVP
    The Founder
    http://www.dotnetfunda.com

    Tripati_tutu
    Posted by: Tripati_tutu | Posted on: 2/28/2011 | Level: Bronze | Status: [Member] | Points: 15 | Alert Moderator 

    Nice one...

    Bugwee
    Posted by: Bugwee | Posted on: 3/1/2011 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

    awesome..

    Brij
    Posted by: Brij | Posted on: 3/5/2011 | Level: Starter | Status: [Member] [Microsoft_MVP] | Points: 15 | Alert Moderator 

    Thanks to all

    Cheers,
    Brij
    Microsoft MVP - ASP.NET/IIS
    http://brijbhushan.net

    Sutotpal
    Posted by: Sutotpal | Posted on: 3/8/2011 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

    Nice blog,,, You solved my problem..
    i also faced this problem.

    Best Regards,
    Sutotpal

    Akiii
    Posted by: Akiii | Posted on: 10/13/2011 | Level: Bronze | Status: [Member] | Points: 15 | Alert Moderator 

    Hi Brij...
    It solved my problem also.....
    Great article...
    keep posting..

    Thanks and Regards
    Akiii

    Hareesh
    Posted by: Hareesh | Posted on: 1/30/2012 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

    Hi Brij
    Your article is nice.

    Thanks
    Hareesh

    Bhupentiwari
    Posted by: Bhupentiwari | Posted on: 7/10/2012 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

    nice section n very good article

    Thanks n Regards
    Bhupendra Tiwari

    Dharanidhar
    Posted by: Dharanidhar | Posted on: 8/28/2012 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

    Nice article..
    Good one..

    >> Write Response - Respond to this post and get points

    More Blogs

    About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
    General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
    Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/20/2013 6:16:53 AM