Link to home
Start Free TrialLog in
Avatar of jtrapat1
jtrapat1

asked on

TextChanged Event for Valid Start and End Dates

This seems like a simple problem but Ive been stuck on it for weeks....

I have two textboxes (along with calendar controls) for start and end date.
I also have an asp:dropdownlist box (of client names) which is loaded by a stored procedure; after the user enters a valid start and end date range.

If the end user changes EITHER date field, I would like to refresh the dropdownlist box.
I simply want to add this little attached piece of coding to the text_changed event for both dates.

But, I keep getting conversion errors from textbox to date format, etc.

Is there an easy way to accomplish this task, without using server side validation or required field validators?

Please reply.
Thanks
John
Protected Sub startdate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles startdate.TextChanged
            If IsDate(Convert.ToDateTime(startdate)) = True Then
                ' We're all set - refresh the client drop down!
                Call load_Client()
            Else
                Return
            End If
        End Sub
 
 
        Protected Sub enddate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles enddate.TextChanged
 
            If IsDate(enddate) = True Then
                ' We're all set - refresh the client drop down!
                Call load_Client()
            Else
                Return
            End If
 
        End Sub

Open in new window

Avatar of tiagosalgado
tiagosalgado
Flag of Portugal image

In what format is the input dates?
Try using
DateTime.TryParseExact instead of Convert.ToDateTime ....

and my pet hate

this
If IsDate(enddate) = True Then

should be
If IsDate(enddate)  Then


;-)
Hi,

Are startdate, and enddate your two textboxes? then I guess you have to get the text value out of it to convert:

If IsDate(Convert.ToDateTime(startdate.Text)) = True Then
You might wanna check if the text is not null of empty first before you do the Convert. A better way is you should define a function (call it IsDateTimeInput)
Function
Try
     If IsDate(Convert.ToDateTime(inpuString)) = True Then
                Return true;              
   End If
 Catch exp As System.Exception
     Return false;
End Try

Return false;
End Function


If you dont want to validate your dates on server side, then you have to use javascript for your 2 text box, the way you do it now is using server post back and check it there (this isn't too bad though).

//CheckDateInput is javascript function
startdate.Attributes.Add("onchange", "return CheckDateInput(" + startDate.ClientID + ");"
enddate.Attributes.Add("onchange", "return CheckDateInput(" + endDate.ClientID + ");"

You can define more javascript function to validate this.
And javascript also support exception handle (try catch) and Date.Parse to check the input

Hope this help



Avatar of jtrapat1
jtrapat1

ASKER

thanks for the replies-
Start Date and End Date are input text boxes next to two calendar controls;
But, Im just worried that the end user may ignore the calendar and enter the dates themselves.
I guess I could do a format in parentheses next to the textbox (MM/DD/YYYY).
But I want to do some error trapping.

Thanks
John
Yes, suggestion is always nice, but when user accidentally or  un-accidentally  input the wrong value, then we dont want to see ugly error page.

Validate them using client javascript is the bestway, otherwise throw exception handling to verify user input. Happy ending ^^

Goodluck

JINN
Or to prevent wrong date input, set your textboxs to read-only and assign only the date clicked on your calendar control.
Ive included a .bmp to try to make it clearer-
I started with seven input fields to run the report-
Now, since I added the onclick  javascript calendars to help load the textfields for start and end date;
the calendars get the focus and its not as easy to tab thru all the required fields and then hit:
View Report.

The most important thing is that the bottom two drop downs are dependent on the above start and end date textfields
these drop downs below should automatically refresh ANYTIME the start and end dates are changed.

Is there any way to accomplish this - I will try the javascript check for now but i just want to be consistent.
Thanks.

John
gui.bmp
ASKER CERTIFIED SOLUTION
Avatar of jinn_hnnl
jinn_hnnl
Flag of Netherlands 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
"Now, since I added the onclick  javascript calendars to help load the textfields for start and end date;
the calendars get the focus and its not as easy to tab thru all the required fields and then hit:
View Report."
You cant use TabIndex on your textboxs to prevent that.
 
Thanks everyone for the help--

I have a lot of it working but a new problem that I think can be fixed.
Now that I covered the case where the user enters a date directly into the "asp:textbox", autopostback is set to true and therefore catches the mistake right away.....good.
(attached is the final code i ended up using.)

Now, I also have href:javascript popup calendars to allow the user to select a date.
The date is selected from the calendar and passed to the start and end date text fields ok but the page does not submit and therefore cannot be validated on the fly like the other validation.
(that code is attached first.)

My question is: can't I force a page submit so that the data will be validated?
Something like: "document.forms[0].submit();"

and the second part of the question is:
where would this code go?
Inside my little calendar popup? After I select the date?

Reply when you can.
John
<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"></asp:TextBox>
                        <a href="javascript:;" onclick="window.open('PopUp.aspx?textbox=startdate','cal','width=250,height=225,left=270,top=180')">
                            <img alt="" src="images/cal.gif" width="16" height="16" /></a> <b><font face="arial"
                                size="2">(MM/DD/YYYY) </font></b>
                    </td>
 
----------------------------------
Protected Sub startdate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles startdate.TextChanged
 
             If enddate.Text <> "" And startdate.Text <> "" Then
                Try
                    If IsDate(Convert.ToDateTime(startdate.Text)) And _
                        IsDate(Convert.ToDateTime(enddate.Text)) Then
 
                        ' We're all set - refresh the client drop down!
                        Call load_Client()
                        lblMessage.Text = "Client Data Has Been Loaded."
                    End If
                Catch exp As System.Exception
                    lblMessage.Text = "Your Start Date Input Is Not in a correct format, please try again."
                End Try
            End If
        End Sub

Open in new window