I need to write a function that can detect whether a string input is a valid format for a date. Allowable formats are:
#### (e.g. 2003)
##/#### (e.g. 12/2003)
#/#### (e.g. 9/2003)
##/####-##/#### (e.g. 12/2003-04/2005)
But I need to prevent invalid strings (e.g. "20031", "ABCD", "200A").
I've written a function to do this in VB already (below), but I need to do it in Javascript.
VB
Public Sub detectDateFormat(ByVal myDate As String)
If myDate Like "####" Then
'Do Stuff 1
ElseIf myDate Like "##/####" Or myDate Like "#/####" Then
'Do Stuff 2
ElseIf myDate Like "##/####-##/####" Then
'Do Stuff 3
Else
'Invalid date format
Exit sub
End If
End Function
It seems javascript has no equivalent to "Like", and it's tricky to detect if a character is a number or letter. Can anyone suggest a good way to do this?