Solved

One Validator to validate multiple controls?

Posted on 2004-03-26
3
5,502 Views
Last Modified: 2008-10-04
Hi, I'm creating a registration page for a website.  On it, the user needs to enter their birthdate.  I have 3 textbox controls, one for month, one ofr day, and one for year for the user to input their birthdate.  Right I have to create 3 seperate RequiredFieldValidator to make sure user put something in those 3 textboxes, then I have 3 different CustomValidator to validate that they have entered the correct dates.  i.e. making sure no1 put 9999 or 1000 as their birth year or put 30 as their birth month.  My question is, is there a way to just have one custom validator to do all that? Instead of checking one TextBox at a time, can I do something to check all 3 at the sametime and just output 1 error message instead of 3 seperate ones? thanks!
0
Comment
Question by:regulatorz
3 Comments
 

Accepted Solution

by:
kingwr12 earned 25 total points
ID: 10693396
You can create one custom validation control to validate the three fields and do not tie it to any control:

<asp:customvalidator id="valBirthDate" clientvalidationfunction="validateBirthDate" display="None" runat="server"/>

You may want to place this control at the end of your form so it is called last in the validation order upon submission.  Then check the individual controls are valid before validating overall.

You can validate client side in a javascript function validateBirthDate:

    // Validate the birth date
    function validateBirthDate(val, args) {
        if(not valid) {
          val.errormessage = "The birth date is not valid because of X, Y, or Z";
          args.IsValid = false;
        }
        else
          args.IsValid = true;            
        return;
    }

and/or on the server side:

Private Sub valBirthDate_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles valBirthDate.ServerValidate

   ' Validate the birth date
    If (not valid) Then
        CType(source, CustomValidator).ErrorMessage = "The birth date is not valid because of X, Y, or Z"
        args.IsValid = False
    Else
        args.IsValid = True
    End If

End Sub

Now, the problem with this is although you will get a custom message in your validation summary control, the little * or whatever will not display next to the individual fields.  You can set the three fields to invalid in the validation function (client or sever side), but then the required message will be displayed along with your custom message.  There is a way to clear the message out and display the *, but it is messy and leaves your control in a weird state until the page is refreshed:

    // Clear the individual required validator message and set to invalid
    with(document.getElementById("valBirthMonth")) {
        isvalid = false;
        errormessage = null;
    }
    ValidatorUpdateDisplay(document.getElementById("valBirthMonth"));

The "ValidatorUpdateDisplay" function is in ASP.NET's WebUIValidation.js file.

You could consider three custom validation functions for the individual fields that performs the required validation as well as the range validation, and then a custom validation function with no control for the overall validation upon submit.  Each validation function would generate it's own specific message (none specified in the HTML) and therefore the state of the controls would not get screwy.  Frankly, we use a proprietary validator (rtg:xvalidator) that performs required validation, type validation, and custom validation all in one and thus circumvents some of these pitfalls.  

Hope this helps,
WRK
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

861 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

24 Experts available now in Live!

Get 1:1 Help Now