CustomValidator Question

Posted on 2011-10-26
Last Modified: 2012-05-12
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." 

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

            args.IsValid = false;



Open in new window

Question by:jazzcatone
    LVL 74

    Accepted Solution

    Check the Page.IsValid property before executing other code. This is set by your args.IsValid = ... assignment.

    Author Closing Comment

    Yep that did it. Thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now