ASP.NET TextBox in Multi Line mode, and (lack of) MaxLength Property

If you ever try to set the MaxLength property of a MultiLine TextBox in ASP.NET, it won't work. It would seem to have no effect at all. 

MaxLength Atrribute is not supported by the TextArea element 

When an ASP.NET TextBox control is set to MultiLine mode, it is rendered as a TextArea HTML element. TextArea element do not support 'MaxLength' attribute, and therefore the TextBox won't render this attribute in MultiLine mode. It won't give you any warning or throw an error, but quietly ignore this property.

Solution 

The first time I had a need to restrict length in a MultiLine TextBox, I quickly dropped a CustomValidator Control to take care of it. But later I realized a ReqularExpressionValidator could also be used for this purpose.

Here is the ValidationExpression to set the MaxLength (and also MinLength) of the input string: [^$]{MinLength,MaxLength}

Replace MinLength and MaxLength with numeric values. For example, a 100 character MaxLength with no Minimum length: [^$]{0,100}

Note:

A new line in MultiLine mode is considered two characters, because a new line is actually combination of two characters: CR and LF. I am not sure but on Unix/Linux based clients this may not be the case.

Afterthought 

Ok, it seems many people end up here looking for a solution to this MaxLength property issue, and looking at the low ratings this post has received, I am assuming that some were not very happy with the above solution.

This post was originally meant to be for developers who would know how to use a RegularExpressionValidator and ASP.NET validation in general.

The behavior with a RegularExpressionValidator does not simulate the MaxLength property exactly because it does not prevent the user from entering further characters once the limit is reached, but rather integrates with the rest of validation framework and shows a friendly message to the user. To simulate the MaxLength property exactly, you could write a small Javascript snippet to trap the OnKeyPress event of the TextArea element and discard further keystrokes once the limit is reached.

Here is an example for keeping MaxLength to 100 characters:

onkeypress="if(this.value.length>=100) return false;"
You can add this straight into the TextBox tag. If javascript is disabled on the client browser, this will simply fail, and so you still need to validate it on server side.

I personally prefer the use of validation controls as it inherently validates both client side and server side.

The solution shown here could also be used with single line TextBox if you needed to ensure a minimum length of input. 

Posted on September 15, 2007 03:44 by Haider

Comments

November 21. 2007 11:30

selwyn cerejo

helped me in my work thanks

selwyn cerejo

December 24. 2007 06:58

Joe Gakenheimer

Yes, the RegEx Validation Control does not do the trick, it seems like a blank line kills the control. The JavaScript solution is better, but still not perfect. You can count the characters, but JavaScript ignores returns, such as someone copies and pastes a couple paragraphs into a word document. Good work though, it helps on smaller multi-lined text boxes.

Joe Gakenheimer

December 24. 2007 07:04

Joe Gakenheimer

here's the answer for the RegEx validator: [\s\S]{0,4000}$

it counts the characters, white-spaces, and lines!

moojjoo.blogspot.com/.../...ine-text-boxes-in.html

Joe Gakenheimer

January 6. 2008 23:04

James

I would not like the white spaces and lines to be counted. Only characters. Is there a RegEx that will do this?

James

January 28. 2008 06:51

dennis field

thanks this was a help,

I used the javascript approach as it was easier for now.  but instead of return false this slight  tweak worked a bit better for my purposes.  
if(this.value.length>=100) this.value = this.value.substring(0, 99);

the other javascript approach essentially locks the textbox, so the user can't backspace.  Still not perfect though as users can cut & paste beyond the limit.

dennis field

March 11. 2008 12:57

Sophia

It is really helpful. I used it on ASP.NET C#. It is really simple solution.

Sophia

April 23. 2008 05:58

vinusoma

Just what I needed to know - thanks.

vinusoma

May 13. 2008 22:42

Avianash

Its really helped me very much......

Avianash

May 21. 2008 01:19

Abhishek Dhanotia

Here i have solution....it will 100% work..
Java Script:  
<script language="Javascript">
function Limit(Object,MaxLen)
{

    if(Object.value.length>MaxLen)
    {
  alert("Max length should be less than or equal to " + MaxLen + ".");
  Object.focus();
    }
}
function MaxLength(Object, MaxLen)
{
      return (Object.value.length < MaxLen);
}
</script>
add it in Text box events:
onkeypress="return MaxLength(this, 50);" onblur="Limit(this,50)"

Abhishek Dhanotia

January 2. 2009 03:29

Ravi Singh

I appriciate the solution
good job

Cheers

Ravi Singh

March 30. 2009 02:20

Avinash

Thanks a Lot!!!!!!!!!!!!!

Avinash

July 11. 2009 15:37

don perry

THis worked : ^[\s\S]{20,2000}$ min 20 max 2000

don perry

July 30. 2009 18:20

Solomon


Thanks for the working solution (JS) provided from Abeshek Dhanotia. Good job!!

Solomon

August 21. 2009 03:53

mets

The problem with this

onkeypress="if(this.value.length>=100) return false;"

is it doesn't stop you cutting and pasting a much larger amount of text into the box which, if not validated server side, could cause an error.

mets

February 2. 2010 23:37

David

Solomon,

I have the same issue - try adding this too:

this assumes maxlength of 150

onkeyup="if(this.value.length>=150) {this.value = this.value.substr(0, 150);  return false;}"

David

May 20. 2010 19:24

http://www.thecosgold.com/

Do you know<a href="www.thechroniclesofspellborngold.com/">tcos Gold</a>?if you play the online game,you will know<a href="www.thechroniclesofspellborngold.com/">chronicles of spellborn gold</a>is the game gold.

http://www.thecosgold.com/

May 20. 2010 19:24

tcos money

Do you know[url=http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com]tcos Gold[/url]?if you play the online game,you will now[url=http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com]chronicles of spellborn gold[/url]is the game gold. In the game,if you had more[url=http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com]the chronicles of spellborn Gold[/url],you will had a tall level. But if you want [url=http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com] tcos money [/url],you can come here and spend a little money to[url=http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com" rel="nofollow">http://www.thechroniclesofspellborngold.com]chronicles of spellborn money[/url].Quickly come here.

tcos money

May 21. 2010 19:51

Snw Gold

Last year, I know the game mixmaster by chance; I try to play it to pass the time. To my surprise, I get some <a href="www.mmodo.com/.../...orld__Vis.html">Sword" rel="nofollow">www.mmodo.com/.../...orld__Vis.html">Sword of the New World Vis </a>, later, my friend told me that how to get more <a href="www.mmodo.com/.../...orld__Vis.html">Sword" rel="nofollow">www.mmodo.com/.../...orld__Vis.html">Sword of the New World Gold</a> is very important. He taught me how to play the game well, and through buy <a href="www.mmodo.com/.../..._World__Vis.html">Snw Gold</a>

Snw Gold

Don't Post SPAM

If you are posting a commment just to get a link, don't waste your time!

I have a sophisticated comment moderation system in place, and your comment will not be posted.

Add comment




biuquote
  • Comment
  • Preview
Loading