Link to home
Start Free TrialLog in
Avatar of jtrapat1
jtrapat1

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of David Robitaille
David Robitaille
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jtrapat1
jtrapat1

ASKER

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
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...
 
You should NOT set TextBox's AutoPostBack = True if you need to use validators.
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

i diden`t noticed that, goot shot, prairiedog!
Your code is fine if you set TextBox's AutoPostBack = False!!!
I just include the test at the end date, but i can`t speek for others...
But, wait a minute, i think he use AutoPostBack to populate it s  dropdownlist...
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.
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...
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.
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???
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
>>>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.
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