Solved

Validation of string date format dd/mm/yyyy

Posted on 2007-11-28
10
774 Views
Last Modified: 2008-02-01
I searched and I found a few regex solution, but is there an alternative 'normal' way of doing that?

Basically need to validate if it is a valid xx/xx/xxxx, month date (jan 31 days april 30days) and if its a leap year or no (for feb 28or29days).
0
Comment
Question by:Sharalynn
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 17

Expert Comment

by:pssandhu
ID: 20367226
This should help you out:
Link: http://www.dotnetspider.com/kb/Article341.aspx

P.
0
 

Author Comment

by:Sharalynn
ID: 20367320
Thanks, however I have the same problem as the guy over there as the code does not work. It just create the day, month, year perfectly even with 1/1/1. It does not throw any exception. Big problem!
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 20367358
TryParseExact sample:
Imports System.Globalization
 
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DateString As String = "31/1/2007"
        Dim dt As Date
        If DateTime.TryParseExact(DateString, "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, dt) Then
            MsgBox(dt.ToString("d"))
        Else
            MsgBox("invalid")
        End If
    End Sub
 
 
End Class

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20367359
Do you really need to be so specific on the date format you accept?
DateTime.TryParse() will tell you whether any string can be converted into a valid DateTime, and if you need to spit it back out in a specific format it's easy enough to then use the correct format string with the resulting DateTime object:

Dim input As String = GetDateTimeFromUser() 'imaginary function to get input
DateTime result;
If DateTime.TryParse(input, result) Then
  ' Success
Else
  Throw New Exception("Invalid DateTime")
End If
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20367371
Oops:  confused some C# and VB in the same snippet.  Bad programmer, no cookie ;)
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20367384
If you change the format string to dd/MM/yyyy then you will see that you get "invalid" for dates like 31/1/2007, but it will validate on 31/01/2007.  You can also specify an array of string formats if you wish to support more than one.  
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20367401
>Do you really need to be so specific on the date format you accept?

Well, how else do you figure out what someone means when they enter 10/11/2007.  Do they mean 11th day 10th month, or 10th month, 11th day?
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20367613
>Well, how else do you figure out what someone means when they enter 10/11/2007

It uses the Culture of the current system to decide how to interpret it, though I concede this can be a problem for web apps where that information may not be available.  
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20367849
I see what you mean, but either way, you still have to be specific in terms of letting the user know which format is accepted.
0
 

Author Comment

by:Sharalynn
ID: 20371726
Thanks, very useful and rather clear. I also used "And DateTime.Today > DateString" if you want to make sure they do not enter a future date!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb.net 2 43
vb.net checkbox 7 47
VB.Net - TypeInitializer Error 25 28
i have to take the screenshot of command prompt? how to do this? 1 37
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question