Click here to Skip to main content
15,883,825 members
Articles / Web Development / ASP.NET
Article

Multiline Textbox Length Validator Control

Rate me:
Please Sign up or sign in to vote.
3.88/5 (9 votes)
14 Apr 20041 min read 138.1K   2.4K   32   23
A validator control that limits the number of characters allowed in a textbox when the mode is set to multiline.

Introduction

It has annoyed me for a while now that the MaxLength attribute does not work for textboxes with the TextMode property set to MultiLine. Although a CustomValidator works for this sort of validation, I wanted something that was easier and quicker to implement. I also wanted some experience of writing controls, so I decided to write my own validator.

Using the Control

The control first needs to be registered on the aspx page:

ASP.NET
<%@ Register TagPrefix="controls" Namespace="Web.Controls" Assembly="Web.Controls" %>

It can then be added to the form:

ASP.NET
<controls:TextLengthValidator id="ValidLength" runat="server" 
    ControlToValidate="TextBox1" MaxLength="10" DisplayCharactersEntered="true" 
    Text="String too long<br>" />

Since the control inherits from BaseValidator, it has many of the same properties as the other .NET validator controls, however there are 2 additions:

  • MaxLength
  • DisplayCharactersEntered

As you can probably guess, MaxLength is the maximum number of characters allowed in the textbox. DisplayCharactersEntered was added because of a request from my boss who wanted the number of characters the user had entered displayed along with the error message. If this property is true, the string (xx characters entered) is appended onto the end of the validator text.

The control also does client side checking on UpLevel browsers. To get this to work though, the file CheckLength.js should be placed in a folder TextLengthValidator at the root of the website. This file is included with the control source.

Conclusion

That's pretty much it for just now. I haven't added any design support, I've still to read up on that. Once I have, you may see an improved version of my little validator. In the meantime, if anyone has any comments, get in touch.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Pretend there is something witty and interesting here.

Comments and Discussions

 
QuestionLength Validator for VB Pin
zach_hurst11-Mar-08 3:07
zach_hurst11-Mar-08 3:07 
GeneralRe: Length Validator for VB Pin
suzyb11-Mar-08 4:24
suzyb11-Mar-08 4:24 
GeneralText Length Validator Pin
igon_ghost6-Aug-07 23:13
igon_ghost6-Aug-07 23:13 
GeneralModified Pin
AnneAtInterpolis18-Dec-06 2:24
AnneAtInterpolis18-Dec-06 2:24 
Generalcustom validation Pin
litson jose5-Nov-06 20:08
litson jose5-Nov-06 20:08 
Generalcustom validation Pin
litson jose5-Nov-06 20:08
litson jose5-Nov-06 20:08 
Generalcustom validation Pin
litson jose5-Nov-06 20:07
litson jose5-Nov-06 20:07 
Generalcustom validation Pin
litson jose5-Nov-06 20:07
litson jose5-Nov-06 20:07 
Generalthanx for the article... another nice solution... Pin
Sina Falahati31-Oct-06 7:56
Sina Falahati31-Oct-06 7:56 
GeneralError Message Pin
rkluth8-Feb-06 8:44
rkluth8-Feb-06 8:44 
Generalby Using Regular Expressions Pin
Usman Shaheen12-Jul-05 0:46
Usman Shaheen12-Jul-05 0:46 
GeneralRe: by Using Regular Expressions Pin
-- m.p.g.11-Aug-05 11:11
-- m.p.g.11-Aug-05 11:11 
GeneralRe: by Using Regular Expressions Pin
reubix1-Oct-05 15:24
reubix1-Oct-05 15:24 
GeneralRe: by Using Regular Expressions Pin
Usman Shaheen2-Oct-05 20:34
Usman Shaheen2-Oct-05 20:34 
GeneralRe: by Using Regular Expressions Pin
Vort3xxX12-Jul-06 9:52
Vort3xxX12-Jul-06 9:52 
Generalanother way... Pin
sergei petrov3-Jun-05 3:41
susssergei petrov3-Jun-05 3:41 
GeneralRe: another way... Pin
walaca18-Jul-05 21:17
walaca18-Jul-05 21:17 
GeneralRe: another way... Pin
jaredo25-Jul-05 8:27
jaredo25-Jul-05 8:27 
QuestionCan it work for non-VS.NET user? Pin
legendgod8-Aug-04 15:58
legendgod8-Aug-04 15:58 
AnswerRe: Can it work for non-VS.NET user? Pin
suzyb10-Aug-04 1:40
suzyb10-Aug-04 1:40 
GeneralGood! But some remarks nevertheless... Pin
Danny Alvares21-Apr-04 14:49
Danny Alvares21-Apr-04 14:49 
GeneralRe: Good! But some remarks nevertheless... Pin
suzyb23-Apr-04 5:42
suzyb23-Apr-04 5:42 
GeneralRe: Good! But some remarks nevertheless... Pin
ewillyb9-Jul-05 7:41
ewillyb9-Jul-05 7:41 

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.