Click here to Skip to main content
15,868,164 members
Articles / Web Development / ASP.NET

Length Validation for all Multiline Textboxes on a Page

Rate me:
Please Sign up or sign in to vote.
4.07/5 (8 votes)
30 Dec 2007CPOL 54.3K   231   23   12
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)


Written By
Austria Austria
Sina Falahati is a Medical student at Vienna Medical University and also student at Technical University of Vienna.

His preferred languages are Music, C++, C#, Turbo Pascal / Delphi, XML, German and Persian.

Comments and Discussions

 
GeneralInvalid Character when run the EncloseScriptBlock Pin
Member 190150028-Apr-11 9:46
Member 190150028-Apr-11 9:46 
GeneralExcellent - Small update Pin
0x29A31-Mar-09 7:59
0x29A31-Mar-09 7:59 
GeneralGood Job! Pin
edusabrz23-Feb-09 12:31
edusabrz23-Feb-09 12:31 
GeneralVB.net version of this code Pin
dolphinpointe10-Sep-08 12:04
dolphinpointe10-Sep-08 12:04 
GeneralRe: VB.net version of this code [modified] Pin
dolphinpointe10-Sep-08 13:48
dolphinpointe10-Sep-08 13:48 
GeneralRe: VB.net version of this code (Working) Pin
DidiKunz7-Aug-12 6:22
DidiKunz7-Aug-12 6:22 
GeneralThanks Pin
Sam.M22-Jan-07 17:59
Sam.M22-Jan-07 17:59 
GeneralError Pin
mishenkovks8-Nov-06 21:30
mishenkovks8-Nov-06 21:30 
AnswerRe: Solved Pin
Sina Falahati9-Nov-06 8:29
Sina Falahati9-Nov-06 8:29 
GeneralThank you Pin
mishenkovks8-Nov-06 3:32
mishenkovks8-Nov-06 3:32 
Generalnice job Pin
Majid Shahabfar7-Nov-06 1:29
Majid Shahabfar7-Nov-06 1:29 
GeneralRe: nice job Pin
Sina Falahati9-Nov-06 8:30
Sina Falahati9-Nov-06 8:30 

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.