Solved

One Validator to validate multiple controls?

Posted on 2004-03-26
3
5,511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

729 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