Results 1 to 3 of 3

Thread: Check the date format input from an asp page

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2000
    Location
    Wolverhampton
    Posts
    20

    Exclamation

    Ello,

    I am writing a registration website, which takes data in and posts it to a database. Problem : if you enter an invalid date into the date of birth field it will kill the programm, is there a way to check the date which is coming into the program via a string to see if it is of a valid format (dd/mm/yy), and if so let the program deal with it.

    Cheers

  2. #2
    Guest
    Normally I'd use the IsDate() function to check the validity of a date (in mm/dd/yy format). I'm not sure how it'd work with your dd/mm/yy format.

    One thing you could do, and this might be excessive (there is probably a better way), is to write your own date checking function. Here's an example:

    Code:
    <%
    Function ValidDOB(dteDOB)
      Dim blnReturn
      Dim varDateParts
      
      blnReturn = False
    
      If InStr(1, dteDOB, "/") > 0 Then
        varDateParts = Split(dteDOB, "/")
        If Ubound(varDateParts) = 2 Then
          If IsDate(varDateParts(1) & "/" & varDateParts(0) & "/" & varDateParts(2)) Then
            blnReturn = True
          End If
        End If
      End If
    
      ValidDOB = blnReturn  
    End Function
    
    If ValidDOB(Request.Form("dob")) Then
      Response.Write "<P>DOB is valid!</P>" & vbcrlf
    Else
      Response.Write "<P>DOB is invalid!</P>" & vbcrlf
    End If
    %>
    This routine would attempt to change a date that's formatted as dd/mm/yy into mm/dd/yy format and check it with IsDate().

    If you are concerned about it being in exactly dd/mm/yy format (like 03/03/68 is valid and 3/3/68 is not) you could add some extra checks on the lengths of the parts of the varDateParts variable. Something like:

    Code:
    Function ValidDOB(dteDOB)
      Dim blnReturn
      Dim varDateParts
      
      blnReturn = False
    
      If InStr(1, dteDOB, "/") > 0 Then
        varDateParts = Split(dteDOB, "/")
        If Ubound(varDateParts) = 2 Then
          If Len(varDateParts(0)) = 2 And Len(varDateParts(1)) = 2 And Len(varDateParts(2)) = 2 Then
    	If IsDate(varDateParts(1) & "/" & varDateParts(0) & "/" & varDateParts(2)) Then
    	  blnReturn = True
    	End If
          End If
        End If
      End If
    
      ValidDOB = blnReturn  
    End Function
    Like I said, there's probably a better way!

    Cheers,
    Paul

  3. #3
    Frenzied Member monte96's Avatar
    Join Date
    Sep 2000
    Location
    Somewhere in AZ
    Posts
    1,379
    There are a few options here, the best of which (IMHO) is to not allow the EU to enter dates in a input tag at all. Use a date picker control.

    You can use the ActiveX dtpicker OR check out Dynamic Drive. They have a javascript calendar control that works pretty well.
    oOOo--oOOo
    __/\/\onte96
    oOOo--oOOo
    Senior Programmer/Analyst
    MCP
    [email protected]
    [email protected]


    Your results may vary.. some restrictions may apply.. pricing and participation may vary.. not available in all states.. professional driver closed course..quantities limited..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width