Solved

One Validator to validate multiple controls?

Posted on 2004-03-26
3
5,500 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

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!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
combo box display member not working 11 21
Get String split 5 33
Spacing between controls 4 16
Red error squiggly on vb.net 7 11
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

18 Experts available now in Live!

Get 1:1 Help Now