Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

One Validator to validate multiple controls?

Posted on 2004-03-26
3
Medium Priority
?
5,524 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 100 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

963 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