How do I suppress the display of validation messages

G

Guest

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?
 
G

Guest

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".
 
J

Jimi200478

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;
}
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top