469,963 Members | 2,039 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

How to Validate a Date Filed.

RSB
Hi Every one

Need some help to Validate the Date i have in the Form.
ALso how to Convert the Date Data i read from table to yyyy/MMM/dd format.

Thanks

RSB
Nov 18 '05 #1
6 1813
You can validate a date using Regular Expressions. It is, by far the easiest
way with validation controls. To validate back end, you can also attempt to
cast into a DateTime structure and see if it blows up. While this may seem a
bit strange, the regex to correctly test for months of different lengths
would be rather large. The DateTime object will guarantee 2/29/2005 does not
slip in.

As far as date formatting, this is most easily changed using CultureInfo
objects and setting culture to a country that uses the date format you wish
to use. The other option is to rip the date apart, by parts, and reorder.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"RSB" <rs*****@hotmail.com> wrote in message
news:YK****************@news.cpqcorp.net...
Hi Every one

Need some help to Validate the Date i have in the Form.
ALso how to Convert the Date Data i read from table to yyyy/MMM/dd format.

Thanks

RSB

Nov 18 '05 #2
You can use the CompareValidator as well. Set the Operator property to
DataTypeCheck and set the Type property to Date.

-John Oakes

"Cowboy" <No************@comcast.netNoSpamM> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
You can validate a date using Regular Expressions. It is, by far the easiest way with validation controls. To validate back end, you can also attempt to cast into a DateTime structure and see if it blows up. While this may seem a bit strange, the regex to correctly test for months of different lengths
would be rather large. The DateTime object will guarantee 2/29/2005 does not slip in.

As far as date formatting, this is most easily changed using CultureInfo
objects and setting culture to a country that uses the date format you wish to use. The other option is to rip the date apart, by parts, and reorder.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"RSB" <rs*****@hotmail.com> wrote in message
news:YK****************@news.cpqcorp.net...
Hi Every one

Need some help to Validate the Date i have in the Form.
ALso how to Convert the Date Data i read from table to yyyy/MMM/dd format.
Thanks

RSB


Nov 18 '05 #3
Here yer go ... as recommended by Cowboy, and cut'n'paste directly from an
app I'm writing.

The first test is for an ordinary date, explicitly coded to en-GB 'cos
browsers don't appear to return the right nationality and this is for UK.
The second test is because the users are occasionally lazy about 4 digit
years, the third test allows for year only entry - a requirement of the app
I'm writing, and the fourth test is just getting fancy; nobody will ever
type the word "today" into a text box. To parse other date formats simply
add more tests.

Cheers

Richard
_____________________________________________
Dr. Richard Petheram
AdlZ Ltd.
E-mail: richard_at_adlz.co.uk
_____________Web: www.adlz.co.uk ____________

[ CODE FOLLOWS ]
Public Shared Function ParseDate(ByVal pString As String, Optional ByRef
IsValid As Boolean = True) As DateTime
Dim lDTF As DateTimeFormatInfo =
CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat
Dim lResult As DateTime

Try
lResult = DateTime.Parse(pString, lDTF).Date
IsValid = True
Return lResult
Catch ex As Exception
End Try

Try
lResult = DateTime.ParseExact(pString, "dd/MM/yy", lDTF).Date
IsValid = True
Return lResult
Catch ex As Exception
End Try

Try
lResult = DateTime.ParseExact(pString, "yyyy", lDTF).Date
IsValid = True
Return lResult
Catch ex As Exception
End Try

If pString.Trim.ToLower = "today" Then
lResult = DateTime.Today.Date
IsValid = True
Return lResult
End If

IsValid = False
Return Nothing
End Function
[ END OF CODE ]
"Cowboy" <No************@comcast.netNoSpamM> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
You can validate a date using Regular Expressions. It is, by far the easiest way with validation controls. To validate back end, you can also attempt to cast into a DateTime structure and see if it blows up. While this may seem a bit strange, the regex to correctly test for months of different lengths
would be rather large. The DateTime object will guarantee 2/29/2005 does not slip in.

As far as date formatting, this is most easily changed using CultureInfo
objects and setting culture to a country that uses the date format you wish to use. The other option is to rip the date apart, by parts, and reorder.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"RSB" <rs*****@hotmail.com> wrote in message
news:YK****************@news.cpqcorp.net...
Hi Every one

Need some help to Validate the Date i have in the Form.
ALso how to Convert the Date Data i read from table to yyyy/MMM/dd format.
Thanks

RSB


Nov 18 '05 #4
RSB
Hi John,
so can i also set the Date format for the CompareValidator???

Thanks
RSB
"John Oakes" <jo**@nospam.networkproductions.net> wrote in message
news:e%***************@TK2MSFTNGP10.phx.gbl...
You can use the CompareValidator as well. Set the Operator property to
DataTypeCheck and set the Type property to Date.

-John Oakes

"Cowboy" <No************@comcast.netNoSpamM> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
You can validate a date using Regular Expressions. It is, by far the easiest way with validation controls. To validate back end, you can also attempt to cast into a DateTime structure and see if it blows up. While this may seem a bit strange, the regex to correctly test for months of different lengths
would be rather large. The DateTime object will guarantee 2/29/2005 does not slip in.

As far as date formatting, this is most easily changed using CultureInfo
objects and setting culture to a country that uses the date format you wish to use. The other option is to rip the date apart, by parts, and reorder.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"RSB" <rs*****@hotmail.com> wrote in message
news:YK****************@news.cpqcorp.net...
Hi Every one

Need some help to Validate the Date i have in the Form.
ALso how to Convert the Date Data i read from table to yyyy/MMM/dd format.
Thanks

RSB



Nov 18 '05 #5
RSB
Hi Richard,
thanks for the help.
So one question do you execute this code as a part of the Validation and
prompt an error message or it just check for the right error format...
"Richard Petheram" <rj*@adlz.co.uk> wrote in message
news:uL***************@newsfe1-gui.server.ntli.net...
Here yer go ... as recommended by Cowboy, and cut'n'paste directly from an
app I'm writing.

The first test is for an ordinary date, explicitly coded to en-GB 'cos
browsers don't appear to return the right nationality and this is for UK.
The second test is because the users are occasionally lazy about 4 digit
years, the third test allows for year only entry - a requirement of the app
I'm writing, and the fourth test is just getting fancy; nobody will ever
type the word "today" into a text box. To parse other date formats simply
add more tests.

Cheers

Richard
_____________________________________________
Dr. Richard Petheram
AdlZ Ltd.
E-mail: richard_at_adlz.co.uk
_____________Web: www.adlz.co.uk ____________

[ CODE FOLLOWS ]
Public Shared Function ParseDate(ByVal pString As String, Optional ByRef
IsValid As Boolean = True) As DateTime
Dim lDTF As DateTimeFormatInfo =
CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat
Dim lResult As DateTime

Try
lResult = DateTime.Parse(pString, lDTF).Date
IsValid = True
Return lResult
Catch ex As Exception
End Try

Try
lResult = DateTime.ParseExact(pString, "dd/MM/yy", lDTF).Date
IsValid = True
Return lResult
Catch ex As Exception
End Try

Try
lResult = DateTime.ParseExact(pString, "yyyy", lDTF).Date
IsValid = True
Return lResult
Catch ex As Exception
End Try

If pString.Trim.ToLower = "today" Then
lResult = DateTime.Today.Date
IsValid = True
Return lResult
End If

IsValid = False
Return Nothing
End Function
[ END OF CODE ]
"Cowboy" <No************@comcast.netNoSpamM> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
You can validate a date using Regular Expressions. It is, by far the easiest way with validation controls. To validate back end, you can also attempt to cast into a DateTime structure and see if it blows up. While this may seem a bit strange, the regex to correctly test for months of different lengths
would be rather large. The DateTime object will guarantee 2/29/2005 does not slip in.

As far as date formatting, this is most easily changed using CultureInfo
objects and setting culture to a country that uses the date format you wish to use. The other option is to rip the date apart, by parts, and reorder.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"RSB" <rs*****@hotmail.com> wrote in message
news:YK****************@news.cpqcorp.net...
Hi Every one

Need some help to Validate the Date i have in the Form.
ALso how to Convert the Date Data i read from table to yyyy/MMM/dd format.
Thanks

RSB



Nov 18 '05 #6
I don't think so. It seems to understand most formats though. I wouldn't
consider it a full-proof method to validate a date, but it works pretty well
depending on what your need is.

-John
"RSB" <rs*****@hotmail.com> wrote in message
news:QV***************@news.cpqcorp.net...
Hi John,
so can i also set the Date format for the CompareValidator???

Thanks
RSB
"John Oakes" <jo**@nospam.networkproductions.net> wrote in message
news:e%***************@TK2MSFTNGP10.phx.gbl...
You can use the CompareValidator as well. Set the Operator property to
DataTypeCheck and set the Type property to Date.

-John Oakes

"Cowboy" <No************@comcast.netNoSpamM> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
You can validate a date using Regular Expressions. It is, by far the easiest
way with validation controls. To validate back end, you can also attempt

to
cast into a DateTime structure and see if it blows up. While this may seem a
bit strange, the regex to correctly test for months of different lengths
would be rather large. The DateTime object will guarantee 2/29/2005 does

not
slip in.

As far as date formatting, this is most easily changed using CultureInfo
objects and setting culture to a country that uses the date format you

wish
to use. The other option is to rip the date apart, by parts, and reorder.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"RSB" <rs*****@hotmail.com> wrote in message
news:YK****************@news.cpqcorp.net...
Hi Every one

Need some help to Validate the Date i have in the Form.
ALso how to Convert the Date Data i read from table to yyyy/MMM/dd

format.
Thanks

RSB



Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by yoshitha | last post: by
4 posts views Thread by Michel Posseth [MCP] | last post: by
3 posts views Thread by samuelberthelot | last post: by
5 posts views Thread by shapper | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.