Click here to Skip to main content
Click here to Skip to main content

Length Validation for all Multiline Textboxes on a Page

, 30 Dec 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Sina Falahati

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 PinmemberMember 190150028-Apr-11 9:46 
GeneralExcellent - Small update PinmemberMember 141888431-Mar-09 7:59 
GeneralGood Job! Pinmemberedusabrz23-Feb-09 12:31 
GeneralVB.net version of this code Pinmemberdolphinpointe10-Sep-08 12:04 
GeneralRe: VB.net version of this code [modified] Pinmemberdolphinpointe10-Sep-08 13:48 
GeneralRe: VB.net version of this code (Working) PinmemberDidiKunz7-Aug-12 6:22 
GeneralThanks PinmemberSam.M22-Jan-07 17:59 
GeneralError Pinmembermishenkovks8-Nov-06 21:30 
AnswerRe: Solved PinmemberSina Falahati9-Nov-06 8:29 
GeneralThank you Pinmembermishenkovks8-Nov-06 3:32 
Generalnice job PinmemberMajid Shahabfar7-Nov-06 1:29 
GeneralRe: nice job PinmemberSina Falahati9-Nov-06 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 30 Dec 2007
Article Copyright 2006 by Sina Falahati
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid