Custom Validator not working without RequiredValidator

Discussion in 'ASP .Net' started by Elie Medeiros via .NET 247, May 18, 2005.

  1. 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.

    --------------------------------
    From: Elie Medeiros

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>07vGoINLaUymVyisY/SgAg==</Id>
    Elie Medeiros via .NET 247, May 18, 2005
    #1
    1. Advertising

  2. Try removing the ControlToValidate property from your CustomValidator. If
    the ControlToValidate is left blank, the validator will fire all the time
    without working with the RequiredFieldValidator.

    You might want to add your own attribute that defines the ControlToValidate
    so you know what control to check on the server.

    HTH,

    bill


    "Elie Medeiros via .NET 247" <> wrote in message
    news:%...
    Hi there,

    I have written a custom validator function to validate a date from a
    user-filled field. The function tries to parse the date and if it can't,
    sets (serverValidateEventArgs)e.IsValid to false.

    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 use that validation control to
    make sure the date a user has entered is valid, without the date field being
    required to complete the form.

    The problem is that this doesn't work - although the CustomValidator itself
    does not show an error message when the field is left blank, the
    ValidationSummary does show the error message, and Page.IsValid==false. The
    most troubling is that I added some logging code to the validation function
    which doesn't seem to get called, suggesting that the validation function
    doesn'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, returning valid=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, returning valid=false", ex);
    return;
    }
    if (dt.Equals(DateTime.MinValue))
    {
    e.IsValid = false;
    logger.Debug("Date to validate==DateTime.MinValue, returning valid=false");
    return;
    }
    logger.Debug("Date to validate is valid ("+dt+"), returning valid=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 as expected
    (ie an error messgage appears both next to the field and in the
    ValidatorSummary)

    Any help greatly appreciated.

    --------------------------------
    From: Elie Medeiros

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>07vGoINLaUymVyisY/SgAg==</Id>
    William F. Robertson, Jr., May 18, 2005
    #2
    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. SStory

    requiredvalidator for checkbox

    SStory, Jul 30, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    1,339
    SStory
    Jul 30, 2003
  2. TJS
    Replies:
    3
    Views:
    546
  3. tshad

    RequiredValidator not working

    tshad, May 20, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    2,005
    Peter Blum
    May 21, 2005
  4. Shimon Sim
    Replies:
    4
    Views:
    22,132
    Patrick.O.Ige
    Feb 3, 2006
  5. Niraj Sikotara
    Replies:
    2
    Views:
    542
    Niraj Sikotara
    Sep 21, 2006
Loading...

Share This Page