ValidationExpression that simulates the MaxLength property

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

As we probably know, the MaxLength property of the TextBox control only
works when the TextMode property is SingleLine or Password. I want to limit
the number of characters a user can enter into a MultiLine TextBox to 250. I
figured the best way to do this would be with a RegularExpressionValidator.
For my ValidationExpression I figured I would use:

..{0,250}

However, the . does not include the \n character. On the page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

there were a bunch of RegexOption members, one of which (the singleline
member) looked like it could do what I wanted. However, I was having trouble
understanding how to specify what options I wanted. If anyone could help me
here, I would appreciate it. Thanks.
 
C

Cor Ligthert [MVP]

Nathan,

Can you explain to us why using a regularExpressionValidator is better than
myTexbox.Text.Lenght > 250

I am curious about that

Cor
 
N

Nathan Sokalski

First of all, it is usually better to use one of the validator controls so
that in the code you can check whether everything passed validation by doing
something like

If Me.IsValid Then
End If

Your technique could be used with a CustomValidator, but CustomValidators
require a PostBack before the user can see the error message and ASP.NET
cannot attempt to generate JavaScript to perform client-side validation.
This is the best way of using one of the validators to limit the number of
characters I could find. Also, using a RegularExpressionValidator is less
code because you do not need to implement a ServerValidate event. In my
opinion, since the TextBox is a server-side control, there should be an
option for having this validation created automatically.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top