Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Length Validation for all Multiline Textboxes on a Page

4.07/5 (8 votes)
30 Dec 2007CPOL 1   232  
How to enable length validation for all Multiline textboxes on a page using a line of code...

Sample Image - MultilineLengthValidator.jpg

Introduction

It is commonly known that the MaxLength attribute does not work for textboxes with the TextMode property set to MultiLine since they are rendered as <textarea> HTML controls, which miss this property.

There are lots of great articles around to solve this, but using custom controls etc., is too time expensive for larger projects.

This approach iterates through all the controls in a page (or other container controls) and attaches a JavaScript to all multiline textboxes to enable length validation. It works with AJAX / ATLAS, FormViews, and all commonly known browsers.

How to use

Add multiline textboxes to your page by setting the MaxLength property. Now, all you have to do is use one line of code...

For performing length validation on all multiline textboxes on a page:

Put TextAreaValidator.CheckMaxLength(this); inside the page's Page_Load function.

For performing length validation on a specific textbox:

Put TextAreaValidator.CheckMaxLength(theTextBox); inside the page's Page_Load function.

For FormViews (also inside ATLAS UpdatePanels), use the FormView's OnItemCreated event with:

TextAreaValidator.CheckAllMaxLengthsWithinParentControl((FormView)sender), since the container controls are created at runtime and previously inaccessible.

Enjoy...

License

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