Announcement

Collapse
No announcement yet.

Check format of input string

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Check format of input string

    Hi all

    If i have an input box that says enter a date in format dd/mm/yyyy. How can i check the user have entered:

    1) in the correct format (i.e. not 10-01-2006 using - instead of /)

    and not as important but woud be nice:

    2) A valid date (i.e. not 45/13/2006)

    Thanks
    Server 2000 MCP
    Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

  • #2
    Re: Check format of input string

    Why not let the user enter the date in whatever valid MS formats are set on his / her machine, test the date, and format it to your liking?

    I imagine you're using VBScript here. Formatting is much weaker than normal VB.

    Try this:

    Option Explicit

    Dim sDateInput, dtDateInput, sDateFormatted

    sDateInput = InputBox("Enter a date")

    If Not IsDate(sDateInput) Then
    MsgBox "Not a valid date"
    ElseIf CDate(sDateInput) <> DateValue(sDateInput) Then
    MsgBox "Has a time component"
    Else
    dtDateInput = DateValue(sDateInput)
    sDateFormatted = Right("0" & DatePart("d", dtDateInput), 2) & "/" & Right("0" & DatePart("m", dtDateInput), 2) & "/" & DatePart("yyyy", dtDateInput)
    MsgBox sDateInput & " - " & dtDateInput & " - " & sDateFormatted
    End If

    Only thing a little whacky is if the user enters 00:00. This isn't trapped by the 2 tests as it is a valid date in MS terms and the time component is 00:00 (same as the a date w/out a time). This translates into the beginning of time in VBScript or December 30, 1899.
    Last edited by rvalstar; 6th November 2006, 16:01. Reason: Forgot to mention the odd case of 00:00
    Cheers,

    Rick

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

    Comment


    • #3
      Re: Check format of input string

      Cheers for that. Much appreciated.
      Server 2000 MCP
      Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

      ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

      Comment


      • #4
        Re: Check format of input string

        I suggest using Regular Expressions
        for more info on regular expressions in VBS: http://msdn.microsoft.com/library/de...ting051099.asp
        and for the date pattern: http://www.regular-expressions.info/dates.html

        This expression example checks the validity of a date (US, but it is easily editable for other format's). Year's 1990-9999, Month's 1 or 01 to 12, Day's 1 or 01 to 31. Still needs to have individual months added (i.e., Feb's 28 days), and some how to check for leap year...the months issue should not be to hard, but the leap year seems like a real chore...
        Code:
        ^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$

        Comment

        Working...
        X