regular expression AND required validator together?

Discussion in 'ASP .Net' started by Keith G Hicks, Feb 21, 2008.

  1. It seems like it would be a reasonably common thing to need to use a regular
    expression validator in combination with a required field validator. I'm
    doing that on a "new password" page. But I'd rather only use one validador.
    Putting 2 validator controls side by side looks funny. For example:

    <asp:TextBox ID="txtUserPwd" runat="server" TextMode="Password"
    Width="230px"></asp:TextBox><asp:RegularExpressionValidator ID="revUserPwd"
    runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password must be
    at least 8 characters in length, contain at least 1 upper case letter, at
    least 1 lower case letter and at least 1 digit."
    ValidationExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$">*</asp:
    RegularExpressionValidator><asp:RequiredFieldValidatorID="rfvUserPwd"
    runat="server" ControlToValidate="txtUserPwd" ErrorMessage="Password is
    required.">*</asp:RequiredFieldValidator></td>

    When the text box is left blank, the asterisk shows up too far to the right
    of the text box. The problem isn't really if there's only one text box to
    validate but if you have 3 or 4 stacked on top of each other. If one text
    box only has a required field validator, then my asterisks don't line up
    vertically. It looks crude that way.

    Bottom line is I need to know if it's possible using regular expressions to
    also require the value. What can I add to this:

    ^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$

    to make it required. I'm not great with regular expressions.

    Thanks,

    Keith
     
    Keith G Hicks, Feb 21, 2008
    #1
    1. Advertisements

  2. ASP.net 2.0 by the way.

    I managed to find a few similar posts and it seems like I'm stuck. Who at MS
    had the limited thinking to NOT include a "required" property in the regular
    expression validator????? Sure seems pretty logical. Hopefully someone has
    an answer. Will wait patiently :)

    Keith
     
    Keith G Hicks, Feb 21, 2008
    #2
    1. Advertisements


  3. Does setting Display="Dynamic" on your validator(s) help? Or swapping the
    placement of the two validators?
     
    Scott Roberts, Feb 21, 2008
    #3
  4. Keith G Hicks

    Jeff Dillon Guest

    I had the exact same situation. I was surprised it wasn't supported
    natively. I wrote a Custom validator (very easy to do...)

    Jeff
     
    Jeff Dillon, Feb 21, 2008
    #4
  5. Setting to Dynamic seems to have solved it. Thanks for the info. :)
     
    Keith G Hicks, Feb 21, 2008
    #5
  6. Jeff,

    Check out Scott Roberts' solution (first person that answered my post). That
    handled my problem fine. Here's the help file info on "Dynamic":

    Dynamic specifies that you want to dynamically place error messages on the
    Web page when validation fails. Space for the validation content is not
    allocated on the page; therefore, the page dynamically changes to display
    the error message. This allows multiple validators to share the same
    physical location on the page. In order to keep the page layout from
    changing when an error message is displayed, the HTML element containing the
    validator must be sized large enough to accommodate the maximum size of the
    validator.



    Keith

     
    Keith G Hicks, Feb 21, 2008
    #6
  7. I don't understand. Since your regular expression rejects any string
    with n < 8 characters, doesn't it already reject any string with 0 < 8
    characters?
     
    Harlan Messinger, Feb 21, 2008
    #7
  8. Keith G Hicks

    Jeff Dillon Guest

    Good to know! Thanks for the kind response...

    Jeff

     
    Jeff Dillon, Feb 21, 2008
    #8
  9. No. They don't work that way. At least not from my experience. I should have
    realized that from working with mask editors in Delphi. Just because
    something's masked (or in this case validated) to have a certain
    configuration of characters, it doesn't mean it can't be null. They're not
    really the same thing. I do still feel though that the regular expression
    validator should have a "required" property so that this is handled in one
    object instead of 2. It's just logical to me since it seems like a common
    need.

     
    Keith G Hicks, Feb 21, 2008
    #9
  10. Hello Keith,
    This has not been done, so that the message you will provide to your user
    is as accurate as possible at th etime the validation fails.

    if it was required, then the message can be: This is a required field.
    if the format is incorrect, then the message can be: The field has an incorrect
    value. It should look like x-y-z.

    They could have put a RequiredText and a RequiredErrorMessage together with
    a FormatText and FormatErrorMessage property on there, but that would have
    broken with all the other validators.

    If you have a customvalidator, you can set the ValidateIfEmpty property to
    true, that way it will still fire your own validation scripts, but that should
    be used to check combinations of different fields or very specific formatting
    issues.

    So that is why.

    Jesse
     
    Jesse Houwing, Feb 21, 2008
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.