Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   How do I suppress the display of validation messages (http://www.velocityreviews.com/forums/t295053-how-do-i-suppress-the-display-of-validation-messages.html)

=?Utf-8?B?UGFjZXIgRGF3bg==?= 05-25-2006 06:09 PM

How do I suppress the display of validation messages
 
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?

Will Asrari 05-25-2006 06:39 PM

Re: How do I suppress the display of validation messages
 
Use 2 validators.


=?Utf-8?B?UGFjZXIgRGF3bg==?= 05-25-2006 08:31 PM

Re: How do I suppress the display of validation messages
 
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".

Jimi200478 05-25-2006 08:55 PM

Re: How do I suppress the display of validation messages
 
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;
}



All times are GMT. The time now is 05:07 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.