Solved

ValidationSummary Control With Date Range, Requiredfield, and Compare Validators.

Posted on 2008-10-31
17
761 Views
Last Modified: 2012-05-05
I would like to get all of these controls to function together (and separately, where the case may be.....)
Attached is my code for the main table of start and end date but I am having trouble with the compare validation and the regularexpressionvalidator-
I have also attached a screenshot of my form.
I would like to get any/all of my validation checks to show up in the validation summary, or on the screen near the control.
Is this possible?
When I enter an invalid date the compare validator picks this up but the message flashes briefly across the form;  It is hidden inside the html tag.  Its like there is a lag when I click the View Report button.
When I click on View Source, I can see the message is written in the html - as "Bad Start Date"

Is there any way I can enable these date validation tags to show up all the time whenever the onblur event occurs, or to get this validation check to show up in the validationsummary?
Does the check have to happen on the onClick() event for the button?
Or, could I hook it to onblur for date validation?

Plus, I need the compare validator to make sure that the end date is greater than the start date.

Unless, someone knows of some javascript I could use to accomplish this???

Thanks

John

<table id="Table9" background="images/cell2_bkgrd.gif" runat="server" border="1"

                cellpadding="3" cellspacing="3" width="100%">

                <tr>

                  <td>

                  

                  <asp:ValidationSummary 

                  id="ValidationSummary1" 

                  DisplayMode="BulletList" 

                  runat="server"

                  HeaderText="Summary of Validation Errors:"

                  Font-Name="verdana"                 

                  Font-Size="9"/>

                  </td> 

                </tr>            

            </table> 

            

            <table id="tblContent" background="images/cell2_bkgrd.gif" runat="server" border="3"

                cellpadding="3" cellspacing="3" width="100%">

                <tr>

                    <td style="width: 340px; height: 30px;">

                        <b><font face="arial" size="2">Start Date:</font></b>

                        <asp:TextBox ID="startdate" runat="server" Width="120px" Text="" TabIndex="1" AutoPostBack="true"

                            MaxLength="10" >

                        </asp:TextBox>

                        <b><font face="arial" size="2">(MM/DD/YYYY) </font></b>

                    </td>

                    <td style="width: 340px; height: 30px;">

                        <b><font face="arial" size="2">End Date:</font></b>

                        <asp:TextBox ID="enddate" runat="server" Width="120px" Text="" TabIndex="2" AutoPostBack="true"

                            MaxLength="10" >

                        </asp:TextBox>

                        <b><font face="arial" size="2">(MM/DD/YYYY) </font></b>

                    </td>

                </tr>

            </table>

        

        <table id="Table8" background="images/cell2_bkgrd.gif" runat="server" border="0"

                cellpadding="3" cellspacing="3" width="100%">

                <tr>

                

                <td width="15%">

                <asp:RequiredFieldValidator 

                        ID="RequiredFieldValidator4" 

                        ControlToValidate="startdate"

                        Enabled="True"

                        Display="static" 

                        ErrorMessage="Start Date Required." 

                        runat="server"

                        SetFocusOnError="True" 

                        Font-Size="Small" />

                        <br /> 

              </td>

                                

                <td width="15%">

             <asp:CompareValidator 

                ID="DateValidator" 

                runat="server" 

                Operator="DataTypeCheck"

                Type="Date" 

                ControlToValidate="startdate" 

                Enabled="True"

                ErrorMessage="Bad Start Date Format" 

                Display="static" />

               <br /> 

                

               </td>

                                

                <td width="15%">

                  <asp:CompareValidator ID="CompareValidator4"

                        ControlToValidate="enddate"

                         ControlToCompare="startdate"

                         Display="static"

                         Text="End Date Must Be Greater Than Start Date!"

                         Operator="GreaterThan"

                         Type="Date"

                         Runat="Server" SetFocusOnError="true" Font-Size="Small" />

                </td>

                

                <td width="15%">

                <asp:RequiredFieldValidator 

                        ID="RequiredFieldValidator5" 

                        ControlToValidate="enddate"

                        Enabled="True"

                        Display="static" 

                        ErrorMessage="End Date Required." 

                        runat="server"

                        SetFocusOnError="True" 

                        Font-Size="Small" />

               

                <asp:CompareValidator 

                ID="CompareValidator1" 

                runat="server" 

                Operator="DataTypeCheck"

                Type="Date" 

                Enabled="True"

                ControlToValidate="enddate" 

                ErrorMessage="Bad End Date Format" />

                </td>

                </tr>

               

           

            </table>

Open in new window

screen.png
0
Comment
Question by:jtrapat1
  • 7
  • 6
  • 4
17 Comments
 
LVL 18

Accepted Solution

by:
David Robitaille earned 450 total points
ID: 22850400
ErrorMessage is the text in the ValidationSummary
Test is the "text" when the validation fail and is displayed at the validator is position. should work on "onblur"
Controls have a CausesValidation property. when set ot true, the validation is trigerred whan you use that control.
0
 

Author Comment

by:jtrapat1
ID: 22850552
davrob60-
thanks for the quick response-

I should have explained a little more about this form-
Its a little complicated because its sort of two parts-
The start date and end date at the top populate the client drop downlist before the bottom fields are filled.

So, if I could do this in an onblur event (validate the date and check if end date is greater than start date) before I click the View Report button, that would be great.
If not, Ill try to do it another way.

But, what bugs me is there is still a lag when the page submits.


Thanks.
John
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22850677
Did you cnow you could use "ValidationGroup"?
if you put a the same validation group on validators and Control, thay will be linked on validation. use that in addition fo the CausesValidation (on the end date and start date) and give some new...
 
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22850827
You should NOT set TextBox's AutoPostBack = True if you need to use validators.
0
 

Author Comment

by:jtrapat1
ID: 22850865
davrob60-
Thanks for all your help-
The page is almost there-
that CausesValidation property was the key -
all of the pages that I looked at for documentation never discussed this method.

One more question-
If I use the compare validator to test if enddate is greater than startdate, could it be attached to both start and end date?
Or do most people just include the test at end date?
For example, I have the attached test at the end date text box:

And I will look into the group validation-
it would be great to combine all of my tests into one.

Thanks again.

<td width="20%">

                  <asp:CompareValidator 

                  ID="CompareValidator4"

                  ControlToValidate="enddate"

                  ControlToCompare="startdate"

                  Display="static"

                  ErrorMessage="End Date Must Be Greater Than Start Date!"

                  Operator="GreaterThan"

                  Type="Date"

                  runat="Server" 

                  SetFocusOnError="true" 

                  Font-Size="X-Small" />

                </td>

Open in new window

0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22850876
i diden`t noticed that, goot shot, prairiedog!
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22850894
Your code is fine if you set TextBox's AutoPostBack = False!!!
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22850907
I just include the test at the end date, but i can`t speek for others...
0
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!

 
LVL 18

Expert Comment

by:David Robitaille
ID: 22850930
But, wait a minute, i think he use AutoPostBack to populate it s  dropdownlist...
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22851026
AutoPostBack and validators do not go together, they contradict each other.
The main reason of using validator is to validate data without having PostBacks. AutoPostBack defeats the purpose.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22851073
ok, but it<s point is th have the data post back if it<s correctly completed. that why the use of a ValidationGroup help.
if all fileds are valid, then the postback appen and the dropdown are populated. if not, then the validation process "block" the postback and the error massage is displayed...
When well used, they dont contradict each other...
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22851211
You misunderstood my point. I said AutoPostBack contradict validator, not PostBack.
Here is what is happening in the author's question:
When user enter a value in one of the TextBox, and click the button. Now, the validator should check all input. But since the TextBox's AutoPostBack is True, so when the user clicks the button, TextBox's TextChanged event fired  and caused a postback, that is why the error message flashes and disappears. Then if you click the button again, everything is fine, it is because no value changes in TextBox so no event fires, and validator works fine.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22851310
And that why a text box with AutoPostBack=true should have CausesValidation=true.
That way, the TextChanged event is not fired when a validator is "in error"
So, jtrapat1, that discution should be instructive to you, but how your things are going???
0
 
LVL 22

Assisted Solution

by:prairiedog
prairiedog earned 50 total points
ID: 22851403
>>>And that why a text box with AutoPostBack=true should have CausesValidation=true.
Sounds right, but please try it to see the result.
0
 

Author Comment

by:jtrapat1
ID: 22851450
Thanks to you both for the discussion-
Since Im paranoid- and since more is more, I set the AutoPostBack=true and CausesValidation=true. and it seems to be working.

Ive had a lot of trouble with this page -since it spun off from a SSRS that we wanted to customize.

Itried it without and it did work prairiedog so you were both correct -

Points rewarded and well worth it!

Thanks
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22851524
>>>I set the AutoPostBack=true and CausesValidation=true. and it seems to be working.
Really? Hmmm, it does not work for me. It always generates a postback and screnn flashes.
Here is what I tested: I enter "qw" in the startdate, then use the tab key to move to the next field, then the error message dispalys and screen flashes and error message goes away.
0
 

Author Comment

by:jtrapat1
ID: 22851557
prairiedog-
You are right- my mistake!
I need the postback to populate the dropdown so I call loadclient() in each _textchanged event of startdate and enddate.
As long as the validation controls confirm before the button is clicked, my page is working.

Thanks again.

John
0

Featured Post

Highfive Gives IT Their Time Back

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

705 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

15 Experts available now in Live!

Get 1:1 Help Now