ValidationExpression that simulates the MaxLength property

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Jan 2, 2007.

  1. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Jan 2, 2007
    #1
    1. Advertising

  2. Nathan,

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

    I am curious about that

    Cor


    "Nathan Sokalski" <> schreef in bericht
    news:...
    > 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.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Cor Ligthert [MVP], Jan 3, 2007
    #2
    1. Advertising

  3. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Cor Ligthert [MVP]" <> wrote in message
    news:O%...
    > Nathan,
    >
    > Can you explain to us why using a regularExpressionValidator is better
    > than
    > myTexbox.Text.Lenght > 250
    >
    > I am curious about that
    >
    > Cor
    >
    >
    > "Nathan Sokalski" <> schreef in bericht
    > news:...
    >> 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.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
    Nathan Sokalski, Jan 3, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWc=?=

    TextBox - MaxLength property

    =?Utf-8?B?bWc=?=, Apr 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,016
    coollzh
    Apr 11, 2004
  2. Jax
    Replies:
    1
    Views:
    3,957
  3. Nathan Sokalski
    Replies:
    2
    Views:
    886
    Nathan Sokalski
    Jan 3, 2007
  4. kalaiselvan
    Replies:
    1
    Views:
    182
    Ken Cox [Microsoft MVP]
    Aug 8, 2003
  5. mg

    TextBox - MaxLength property

    mg, Apr 11, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    375
    Scott Mitchell [MVP]
    Apr 16, 2004
Loading...

Share This Page