Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to Validate a Date Filed.

Reply
Thread Tools

How to Validate a Date Filed.

 
 
RSB
Guest
Posts: n/a
 
      05-20-2004
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


 
Reply With Quote
 
 
 
 
Cowboy
Guest
Posts: n/a
 
      05-20-2004
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" <(E-Mail Removed)> wrote in message
news:YK7rc.1997$(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
John Oakes
Guest
Posts: n/a
 
      05-20-2004
You can use the CompareValidator as well. Set the Operator property to
DataTypeCheck and set the Type property to Date.

-John Oakes

"Cowboy" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:YK7rc.1997$(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Richard Petheram
Guest
Posts: n/a
 
      05-20-2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:YK7rc.1997$(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
RSB
Guest
Posts: n/a
 
      05-21-2004
Hi John,
so can i also set the Date format for the CompareValidator???

Thanks
RSB
"John Oakes" <(E-Mail Removed)> wrote in message
news:e%(E-Mail Removed)...
You can use the CompareValidator as well. Set the Operator property to
DataTypeCheck and set the Type property to Date.

-John Oakes

"Cowboy" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:YK7rc.1997$(E-Mail Removed)...
> > 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
> >
> >

>
>




 
Reply With Quote
 
RSB
Guest
Posts: n/a
 
      05-21-2004
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" <(E-Mail Removed)> wrote in message
news:uL9rc.410$(E-Mail Removed)...
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:YK7rc.1997$(E-Mail Removed)...
> > 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
> >
> >

>
>




 
Reply With Quote
 
John Oakes
Guest
Posts: n/a
 
      05-21-2004
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" <(E-Mail Removed)> wrote in message
news:QVqrc.2055$(E-Mail Removed)...
> Hi John,
> so can i also set the Date format for the CompareValidator???
>
> Thanks
> RSB
> "John Oakes" <(E-Mail Removed)> wrote in message
> news:e%(E-Mail Removed)...
> You can use the CompareValidator as well. Set the Operator property to
> DataTypeCheck and set the Type property to Date.
>
> -John Oakes
>
> "Cowboy" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:YK7rc.1997$(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validate Date AND Tme not just date Chris Davoli ASP .Net Web Controls 0 01-28-2009 09:27 PM
setting date format to validate date in ruby Jay Pangmi Ruby 4 08-25-2008 03:57 PM
how to compare current date to a future date and validate it in ASP.NET James P. ASP .Net 7 07-13-2004 10:13 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM



Advertisments