date drop down list validation

son

Regular
Joined
Feb 21, 2004
Messages
67
hi have have 3 drop down lists one for day one for month and one for year... i would like to add validation to the user so that if for example they select from the drop down list the date 31st february it will tell them that it is an invalid date...

anyone have any suggestions for me.. thanks in advance sonia...
 
You have to use a client script to do that.

You can do it another way, when the user selects a month, fill the days ddl dynamicaly, this way the user won't be able to do a wrong selection, but it would require an additional postback to the server...
 
hi i am trying to do it this way but it keeps on giving me errors that i have my input string in an incorrect format... can some one please help... getting really frustrated me been on it all day..

my code for the drop down lists is as follows

Visual Basic:
  Private Sub ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.Init

        Dim i As Integer
        For i = 1 To 12
            ddlMonth.Items.Add(MonthName(i).ToString)
        Next
        ddlMonth.SelectedIndex = Date.Now.Month - 1
    End Sub


    Private Sub ddlYear_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlYear.Init

        Dim i As Integer
        For i = Date.Now.Year - 5 To Date.Now.Year + 5
            ddlYear.Items.Add(i)
        Next
        ddlYear.SelectedIndex = Date.Now.Year - (Date.Now.Year - 5)
    End Sub

the code i have to save the changes is as follows

Visual Basic:
 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click


        Dim a1 As Integer = ddlMonth.SelectedIndex
        If ddlDay.SelectedValue = "31" Then
            If a1 = 1 Or a1 = 3 Or a1 = 5 Or a1 = 8 Or a1 = 10 Then
                Response.Write("<script>alert('Invalid Date')</script>")
            End If
        End If

               Dim mydate As String

        mydate = ddlDay.SelectedItem.Text + "/" + ddlMonth.SelectedItem.Text + "/" + ddlYear.SelectedItem.Text
        Dim a As DateTime = mydate


        Dim SqlSPParam2 As String = "@news_TitleEN"
        Dim SqlSPParam3 As String = "@news_TextEN"
        Dim SqlSPParam8 As String = "@news_dt"

        Dim SqlParams As SqlParameter() = {New SqlParameter(SqlSPParam3, TextBox3.Text), New SqlParameter(SqlSPParam3, TextBox2.Text), New SqlParameter(SqlSPParam8, a)}
        WfSqlDataPump.ExecuteNonQuery(connectionString, "sp_addnews", SqlParams)

        lblMessage.Text = "The News has been saved!"
          

    End Sub

when i am debbugging the code it gets stuck where i have the a variable which is declared as a string and tells me that i have the input string in invalid format.....

thanks in advance sonia.. ;)
 
hi thanks for ur reply i did try what u recommended but i still keep on getting the error - Cast from string "31/6/2004" to type 'Date' is not valid.

i keep on getting the error cause since i am putting the values that i have in the three dropdownlists into a string and then passing it to datetime variable it brings this error that i have mentioned above.

the error is cropping up in the code below where i have the "a" declared as datetime. its not accepting the string of "mydate" variable to be put into the textbox

Visual Basic:
  Dim a1 As Integer = ddlMonth.SelectedIndex
        If ddlDay.SelectedValue = "31" Then
            If a1 = 1 Or a1 = 3 Or a1 = 5 Or a1 = 8 Or a1 = 10 Then
                Response.Write("<script>alert('Invalid Date')</script>")
            End If
        End If

               Dim mydate As String

        mydate = ddlDay.SelectedItem.Text + "/" + ddlMonth.SelectedItem.Text + "/" + ddlYear.SelectedItem.Text

        Dim a As DateTime = mydate

textbox1.text = a
do u know how i can go around this.. thanks in advance..;)
 
Last edited:
Hi..

I have a few questions on your code. Can you paste the exact error?

One of the questions is the usage of '+' for string concatenation. I think you need to use '&' for that.

SJ


son said:
hi thanks for ur reply i did try what u recommended but i still keep on getting the error - Cast from string "31/6/2004" to type 'Date' is not valid.

i keep on getting the error cause since i am putting the values that i have in the three dropdownlists into a string and then passing it to datetime variable it brings this error that i have mentioned above.

the error is cropping up in the code below where i have the "a" declared as datetime. its not accepting the string of "mydate" variable to be put into the textbox

Visual Basic:
  Dim a1 As Integer = ddlMonth.SelectedIndex
        If ddlDay.SelectedValue = "31" Then
            If a1 = 1 Or a1 = 3 Or a1 = 5 Or a1 = 8 Or a1 = 10 Then
                Response.Write("<script>alert('Invalid Date')</script>")
            End If
        End If

               Dim mydate As String

        mydate = ddlDay.SelectedItem.Text + "/" + ddlMonth.SelectedItem.Text + "/" + ddlYear.SelectedItem.Text

        Dim a As DateTime = mydate

textbox1.text = a
do u know how i can go around this.. thanks in advance..;)
 
Back
Top