E
Elie Medeiros via .NET 247
Hi there,
I have written a custom validator function to validate a datefrom a user-filled field. The function tries to parse the dateand if it can't, sets (serverValidateEventArgs)e.IsValid tofalse.
The twist is that it allows blank dates to be entered - viz,e.IsValid==true if the field is left blank. The idea is to usethat validation control to make sure the date a user has enteredis valid, without the date field being required to complete theform.
The problem is that this doesn't work - although theCustomValidator itself does not show an error message when thefield is left blank, the ValidationSummary does show the errormessage, and Page.IsValid==false. The most troubling is that Iadded some logging code to the validation function which doesn'tseem to get called, suggesting that the validation functiondoesn't actully get called...
The validation function is as follows:
----------------
public static void ValidateShortDate(object sender,ServerValidateEventArgs e)
{
logger.Debug("Trying to validate a date");
if(StringUtil.IsEmpty(e.Value))
{
e.IsValid=true;
logger.Debug("Date to validate is empty, returningvalid=true");
return;
}
DateTime dt = DateTime.MinValue;
try
{
dt = DateTime.ParseExact(e.Value.Trim(), "d",CultureInfo.CurrentCulture);
}
catch (Exception ex)
{
e.IsValid = false;
logger.Debug("Error parsing date to validate, returningvalid=false", ex);
return;
}
if (dt.Equals(DateTime.MinValue))
{
e.IsValid = false;
logger.Debug("Date to validate==DateTime.MinValue, returningvalid=false");
return;
}
logger.Debug("Date to validate is valid ("+dt+"), returningvalid=true");
e.IsValid = true;
}
----------------
and in the aspx page:
<asp:TextBox runat="server" id="_dateOfBirthCalendar" />
<!--
<asp:RequiredFieldValidator ID="_dateOfBirthValidator1"ControlToValidate="_dateOfBirthCalendar" Runat="server"
ErrorMessage="Please enter a valid birth date" />
-->
<asp:CustomValidator ID="_dateOfBirthValidator"ControlToValidate="_dateOfBirthCalendar"
Runat="server" ErrorMessage="Please enter a valid birth date"/>
----------------
I replaced a custom control by the TextBox in this example.
Note that uncommenting the RequireValidator, the code performs asexpected (ie an error messgage appears both next to the fieldand in the ValidatorSummary)
Any help greatly appreciated.
I have written a custom validator function to validate a datefrom a user-filled field. The function tries to parse the dateand if it can't, sets (serverValidateEventArgs)e.IsValid tofalse.
The twist is that it allows blank dates to be entered - viz,e.IsValid==true if the field is left blank. The idea is to usethat validation control to make sure the date a user has enteredis valid, without the date field being required to complete theform.
The problem is that this doesn't work - although theCustomValidator itself does not show an error message when thefield is left blank, the ValidationSummary does show the errormessage, and Page.IsValid==false. The most troubling is that Iadded some logging code to the validation function which doesn'tseem to get called, suggesting that the validation functiondoesn't actully get called...
The validation function is as follows:
----------------
public static void ValidateShortDate(object sender,ServerValidateEventArgs e)
{
logger.Debug("Trying to validate a date");
if(StringUtil.IsEmpty(e.Value))
{
e.IsValid=true;
logger.Debug("Date to validate is empty, returningvalid=true");
return;
}
DateTime dt = DateTime.MinValue;
try
{
dt = DateTime.ParseExact(e.Value.Trim(), "d",CultureInfo.CurrentCulture);
}
catch (Exception ex)
{
e.IsValid = false;
logger.Debug("Error parsing date to validate, returningvalid=false", ex);
return;
}
if (dt.Equals(DateTime.MinValue))
{
e.IsValid = false;
logger.Debug("Date to validate==DateTime.MinValue, returningvalid=false");
return;
}
logger.Debug("Date to validate is valid ("+dt+"), returningvalid=true");
e.IsValid = true;
}
----------------
and in the aspx page:
<asp:TextBox runat="server" id="_dateOfBirthCalendar" />
<!--
<asp:RequiredFieldValidator ID="_dateOfBirthValidator1"ControlToValidate="_dateOfBirthCalendar" Runat="server"
ErrorMessage="Please enter a valid birth date" />
-->
<asp:CustomValidator ID="_dateOfBirthValidator"ControlToValidate="_dateOfBirthCalendar"
Runat="server" ErrorMessage="Please enter a valid birth date"/>
----------------
I replaced a custom control by the TextBox in this example.
Note that uncommenting the RequireValidator, the code performs asexpected (ie an error messgage appears both next to the fieldand in the ValidatorSummary)
Any help greatly appreciated.