Click here to Skip to main content
6,295,667 members and growing! (14,763 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Intermediate

Multiline Textbox Length Validator Control

By suzyb

A validator control that limits the number of characters allowed in a textbox when the mode is set to multiline.
C#, Windows, .NET 1.1, ASP.NET, VS.NET2003, Dev
Posted:14 Apr 2004
Views:90,447
Bookmarked:27 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
8 votes for this article.
Popularity: 3.43 Rating: 3.80 out of 5

1
2 votes, 25.0%
2
1 vote, 12.5%
3
1 vote, 12.5%
4
4 votes, 50.0%
5

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:

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

It can then be added to the form:

<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

About the Author

suzyb


Member
Suzanne developed an interest in computing after watching an episode of 'Lois &amp; Clark: The New Adventures of Superman' in which a deranged megalomaniac tries to take over the world with a computer.

Now years later she is ready to put in place her own plan for world domination using an unrealistic virtual reality system and a bicycle helmet.
Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular Custom Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
QuestionLength Validator for VB Pinmemberzach_hurst4:07 11 Mar '08  
GeneralRe: Length Validator for VB Pinmembersuzyb5:24 11 Mar '08  
GeneralText Length Validator Pinmemberigon_ghost0:13 7 Aug '07  
GeneralModified PinmemberAnneAtInterpolis3:24 18 Dec '06  
Generalcustom validation Pinmemberlitson jose21:08 5 Nov '06  
Generalcustom validation Pinmemberlitson jose21:08 5 Nov '06  
Generalcustom validation Pinmemberlitson jose21:07 5 Nov '06  
Generalcustom validation Pinmemberlitson jose21:07 5 Nov '06  
Generalthanx for the article... another nice solution... PinmemberSina Falahati8:56 31 Oct '06  
GeneralError Message Pinmemberrkluth9:44 8 Feb '06  
Generalby Using Regular Expressions PinmemberUsman Shaheen1:46 12 Jul '05  
GeneralRe: by Using Regular Expressions Pinmember-- Mike12:11 11 Aug '05  
GeneralRe: by Using Regular Expressions Pinmemberreubix16:24 1 Oct '05  
GeneralRe: by Using Regular Expressions PinmemberUsman Shaheen21:34 2 Oct '05  
GeneralRe: by Using Regular Expressions PinmemberVort3xxX10:52 12 Jul '06  
Generalanother way... Pinsusssergei petrov4:41 3 Jun '05  
GeneralRe: another way... Pinmemberwalaca22:17 18 Jul '05  
GeneralRe: another way... Pinmemberjaredo9:27 25 Jul '05  
GeneralCan it work for non-VS.NET user? Pinmemberlegendgod16:58 8 Aug '04  
GeneralRe: Can it work for non-VS.NET user? PinmemberSuzanne Boyle2:40 10 Aug '04  
GeneralGood! But some remarks nevertheless... PinmemberDanny Alvares15:49 21 Apr '04  
GeneralRe: Good! But some remarks nevertheless... PinmemberSuzanne Boyle6:42 23 Apr '04  
GeneralRe: Good! But some remarks nevertheless... Pinmemberewillyb8:41 9 Jul '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Apr 2004
Editor: Smitha Vijayan
Copyright 2004 by suzyb
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project