How do I suppress the display of validation messages

Discussion in 'ASP .Net' started by =?Utf-8?B?UGFjZXIgRGF3bg==?=, May 25, 2006.

  1. I have a web control that takes a date input. There are two types of
    validation I want to do on this:

    1) validate that the date input is in the proper format, and display a
    message if otherwise
    2) validate that the date is not prior to today, and display a message if
    otherwise.

    For these validations, I use CompareValidator to ensure the date is not
    prior to today, and RegularExpressValidator to ensure the date is in the
    proper format (mm/dd/yyyy).

    However, it appears that both of these validators fire and display. So, if
    the user enteres in a bogus date (ABCDEFG), they get both the error message
    that the date is in the improper format AND that the date is prior to today.

    If the date isn't formatted corretly, I don't want the "prior to today"
    validation to fire. But I can't seem to do that, and my page always displays
    both error messages.

    Is there any way to do this?
     
    =?Utf-8?B?UGFjZXIgRGF3bg==?=, May 25, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?UGFjZXIgRGF3bg==?=

    Will Asrari Guest

    Use 2 validators.
     
    Will Asrari, May 25, 2006
    #2
    1. Advertising

  3. Thanks for the reply, but unfortunately I'm not quite sure what you mean. I
    mean, I am using two validators, but the problem is both are displaying their
    error messages and I only want the first error to display. For example, here
    is the code:

    <asp:RegularExpressionValidator
    ID="MyDateRegularExpressionValidator"
    runat="server"
    ControlToValidate="MyDateTextBox"
    ValidationExpression="(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[-
    /.](19|20)\d\d"
    Display="Dynamic"
    Text="Improper date">
    </asp:RegularExpressionValidator>

    <asp:CompareValidator
    ID="MyDateAfterTodayCompareValidator" runat="server"
    ControlToCompare="TodaysDateTextBox"
    ControlToValidate="MyDateTextBox"
    Display="Dynamic"
    Operator="GreaterThanEqual"
    Type="Date"
    Text="Date is prior to today">
    </asp:CompareValidator>

    If I enter "asdf" in the field, the web page displays two error messages:

    Improper date
    Date is prior to today

    I want it to stop after displaying "Improper date" and not display "Date is
    prior to today".
     
    =?Utf-8?B?UGFjZXIgRGF3bg==?=, May 25, 2006
    #3
  4. =?Utf-8?B?UGFjZXIgRGF3bg==?=

    Jimi200478 Guest

    i would suggest doing custom validation instead of using the validation
    that ASP.NET provides. Here is a sample of a date validator.

    private bool ValidateDate(string Date, Label ErrLabel)
    {
    bool DateIsValid = false;
    ErrLabel.CssClass = "ErrMsg";
    ErrLabel.Text = string.Empty;
    string DateRegex =
    @"^(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/\d{4}$";

    //check to see if format is correct
    if(FormatValid(Date, DateRegex))
    {
    try
    {
    DateTime.Parse(Date);
    DateIsValid = true;
    }
    catch
    { ErrLabel.Text = "Format: 'MM/dd/yyyy'"; }
    }
    else
    ErrLabel.Text = "Format: 'MM/dd/yyyy'";

    return DateIsValid;
    }
     
    Jimi200478, May 25, 2006
    #4
    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. Clive
    Replies:
    0
    Views:
    3,008
    Clive
    Aug 12, 2003
  2. Replies:
    1
    Views:
    388
    =?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=
    Aug 22, 2007
  3. Replies:
    6
    Views:
    763
  4. Don Norcott
    Replies:
    13
    Views:
    197
    Don Norcott
    Oct 5, 2010
  5. Nene
    Replies:
    1
    Views:
    132
    ilovelinux
    Mar 15, 2011
Loading...

Share This Page