Click here to Skip to main content
13,000,022 members (44,383 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 27 Sep 2010

Validate Max Length of Multi-Line Textbox

, 27 Sep 2010
Rate this:
Please Sign up or sign in to vote.
This post shows how to validate the max length of a multi-line textbox.


ASP.NET has an uncanny knack for swallowing up huge chunks of my day on silly little things.

After setting the MaxLength properties on all my text boxes before putting my app into test, I was shocked to hear the testers were able to put as much data as they liked into some fields, and were getting database errors about data being truncated.

Sure enough... the MaxLength property doesn't work on Multi-Line Textboxes. A fact that apparently I'm one of the last to know, but heh! I'm a reluctant ASP.NET programmer and where I come from MaxLength works dammit.

So... to save someone else wasting a morning, here's my solution:

  1. Add the following JavaScript to your page:
    <script language="javascript" type="text/javascript">
    function CommentsMaxLength(text,maxLength)
       text.value = text.value.substring(0,maxLength);
  2. Add the following to the definition of your textbox:
    onKeyUp="CommentsMaxLength(this,100)" onChange="CommentsMaxLength(this,100)"

    And that's it. Change the 100 to whatever Maxlength you'd like.

Bonus Points

This part is optional, but I like it, it gives a count down of the number of characters you have left.

  1. Add a label over the text box in question. e.g. a label called 'lblComments' if your text box stores comments.
    Note the name, we'll need it later.
  2. Change the JavaScript function above to the following:
    <script language="javascript" type="text/javascript">
    function CommentsMaxLength(text,maxLength)
        var diff = maxLength - text.value.length;
        if (diff < 0){
            text.value = text.value.substring(0,maxLength);
        document.getElementById('<%= lblComments.ClientID %>').innerText = 
    						"Comments (" + diff + ")";

Note the Name of your label is needed for getElementById.

The rest is the same.


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


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

GeneralThere is a tiny bug Pin
Xmen W.K.9-Oct-10 15:15
memberXmen W.K.9-Oct-10 15:15 
GeneralBigger bug Pin
Xmen W.K.9-Oct-10 15:46
memberXmen W.K.9-Oct-10 15:46 
GeneralRe: Bigger bug Pin
Richard A. Dalton11-Oct-10 1:02
memberRichard A. Dalton11-Oct-10 1:02 
GeneralRe: Bigger bug Pin
Xmen W.K.11-Oct-10 1:56
memberXmen W.K.11-Oct-10 1:56 

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
Web02 | 2.8.170624.1 | Last Updated 27 Sep 2010
Article Copyright 2010 by Richard A. Dalton
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid