Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

One Validator to validate multiple controls?

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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