Link to home
Start Free TrialLog in
Avatar of Jason Livengood
Jason LivengoodFlag for United States of America

asked on

CustomValidator Question

I have a textbox where date of birth is entered. Requirement is that the date of birth be at least 16 years of age. So I have a custom validator with an
OnServerValidate method that does a comprison and returns whether the args.isvalid property is true or false. Now obviously if the method returns a false I want processing to stop and a message to be shown to the user that the dob need to be a least 16 years.

The problem I have right now is not that the code doesn't work right. It does work.(When I set break points and watch it execute, everything seems to run as planned) However, the page just continues to process ! No good ! It just runs through my OnServerValidate method, evaluates to a false, and then keeps going. It eventually redirects to a new page because thats what happens in the subsequent  events. How do Iactually get this to stop processing when the  OnServerValidate method evaluates to false? What am I missing here?
//from .aspx markup

 <asp:TextBox ID="tbDOB1" runat="server" Width="35%"  MaxLength="10" />
 <asp:CustomValidator ID="CheckAgeValidator" runat="server" ControlToValidate="tbDOB1" CssClass="FieldValidatorError"  ErrorMessage="Age must be greater than or equal to 16 years." 
onservervalidate="CheckAgeValidator_ServerValidate"></asp:CustomValidator>

//from codebehind

    protected void CheckAgeValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        DateTime enteredAge = DateTime.Parse(tbDOB1.Text);

        // is the person at least 16
        if (DateTime.Now.Date.AddYears(-16) >= enteredAge)
        {
            args.IsValid = true;
            return;
        }
        else
        {

            
            args.IsValid = false;


       } 

        
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason Livengood

ASKER

Yep that did it. Thanks