Checking If Valid Date entered. C#

3,842 views
Skip to first unread message

Nico VanHaaster

unread,
Dec 20, 2007, 12:57:13 PM12/20/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hey all,

I have been playing around with methods in C# to check if a date is
valid and built a small class to test it. Can some one tell me if this
way will cause problems or if there is a more efficient way.

Code

Class::
public bool isValidDate(string dateString)
{
bool result = true;
try
{
DateTime tDate = Convert.ToDateTime(dateString);
}
catch (Exception ex)
{
result = false;
}
return result;
}

cs Page script
bool validDate = isValidDate(dateBox.Text);

Peter Groenewegen

unread,
Dec 21, 2007, 4:19:42 AM12/21/07
to DotNetDe...@googlegroups.com
You can use TryParse

Cerebrus

unread,
Dec 21, 2007, 5:41:59 AM12/21/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I don't see how you can validate a date until you have some clue about
the cultureinfo, or atleast the format of the entered date.

10/13/2007 would be a valid date in the en-US culture, but not
according to the en-GB culture.

Andrew Badera

unread,
Dec 21, 2007, 5:45:29 AM12/21/07
to DotNetDe...@googlegroups.com
The OP has already half-implemented TryParse as it is ...

Cerebrus -- why do you need to know culture? Let the framework handle it. You don't need to know a thing about it -- TryParse is the way to go, culture is under the hood.

--Andrew Badera

Michael O

unread,
Dec 21, 2007, 4:05:10 PM12/21/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I believe that TryParse has significantly less overhead on mismatches
than throwing/catching an exception.

On Dec 21, 4:45 am, "Andrew Badera" <and...@badera.us> wrote:
> The OP has already half-implemented TryParse as it is ...
>
> Cerebrus -- why do you need to know culture? Let the framework handle it.
> You don't need to know a thing about it -- TryParse is the way to go,
> culture is under the hood.
>
> --Andrew Badera
>
> On 12/21/07, Peter Groenewegen <pgro...@gmail.com> wrote:
>
>
>
> > You can use TryParse
>

Charles A. Lopez

unread,
Dec 21, 2007, 8:36:41 PM12/21/07
to DotNetDe...@googlegroups.com
need some sample input.
 
for example
 
string data is 12/11/2007
 
Is this to be interpreted as December 11 or November 12?
 
Your code seems adequate for now.

 
charle...@gmail.com

Start your career with Charles A. Lopez TECHNOLOGY - Send your resume to charle...@gmail.com

http://cs.nyu.edu/web/People/alumni.html

Marra

unread,
Dec 21, 2007, 10:54:40 PM12/21/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I had to do this on a microcontroller project once.

Its not a massive amount of code but clearly you need to know when a
leap year occurrs and how many days are in each month.

A routine that returns true or false for a given year, month and date
is all that is needed.


Sagar

unread,
Dec 22, 2007, 12:07:19 AM12/22/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Code you are using is server side. Its always a good practice to use
client side. Use following code

<asp:CompareValidator ID="DateValidator" runat="server"
ErrorMessage="Invalid date" ControlToValidate="dateBox.Text"
Operator="DataTypeCheck" Type="Date"></
asp:CompareValidator>


On Dec 20, 10:57 pm, Nico VanHaaster <nvanhaas...@caitele.com> wrote:

Cerebrus

unread,
Dec 22, 2007, 12:21:13 AM12/22/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Yes, but how would the framework determine what a given user means by
(as in my example) a string such as "10/13/2007", if you don't give it
a hint about the culture ?

Marra

unread,
Dec 22, 2007, 4:28:56 AM12/22/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
if year is not a leap year then
if month = jan then
maxdate is 31
if month=feb then
maxdate is 28
if month=fed && leap=1 then
maxdate=29

etc

if date input >maxdate then an error has occurred


what did people do before microsoft wrote the code for them ????????

.

Cerebrus

unread,
Dec 22, 2007, 6:34:59 AM12/22/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Two things :

1. Plow fields.
2. Make drawings on cave walls.

;-)

Andrew Badera

unread,
Dec 22, 2007, 7:36:57 AM12/22/07
to DotNetDe...@googlegroups.com
It's going to default to your main thread's culture to begin with. The only time you need to worry about setting culture info is when the application is executing on a machine whose default system culture is not what the user wants to utilize. Now sure, if there are localization or internationalization concerns, absolutely set your culture, but otherwise, run as client default.




On 12/22/07, Cerebrus <zor...@sify.com> wrote:

Yes, but how would the framework determine what a given user means by
(as in my example) a string such as "10/13/2007", if you don't give it
a hint about the culture ?

Andrew Badera

unread,
Dec 22, 2007, 7:40:03 AM12/22/07
to DotNetDe...@googlegroups.com
Why do so many on this group seem assume all .NET code is ASP.NET? Sheesh! :P

Yes, absolutely, validate at the client, if it's a form or web app. But maybe it's a batch process of some sort, or some other sort of backend processing.

--Andrew Badera




On 12/22/07, Sagar <sagar.t...@gmail.com> wrote:

Code you are using is server side. Its always a good practice to use
client side. Use following code

<asp:CompareValidator ID="DateValidator" runat="server"
ErrorMessage="Invalid date" ControlToValidate=" dateBox.Text"
            Operator="DataTypeCheck" Type="Date"></
asp:CompareValidator>


Michael O

unread,
Dec 22, 2007, 12:23:31 PM12/22/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
This is sarcasm, right? If you write even a single line of code that
duplicates the DateTime and Timespan structs, that code is an
abortion.

Cerebrus

unread,
Dec 22, 2007, 8:02:59 PM12/22/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
My point exactly ! But all too often, users neglect to even set their
"regional options", which would mean that everything is "en-US" by
default. Also, that is only valid for Windows apps. Since the OP
mentioned "cs Page script ", I assumed it was a Web app, in which
case, I reiterate, you would need some kinda hint towards CultureInfo.

Peter Groenewegen

unread,
Dec 23, 2007, 4:26:40 AM12/23/07
to DotNetDe...@googlegroups.com
A good thing is to hint the user about the format: mm-dd-yyyy or dd-mm-yyyy. You can base the hint on the culture of the calling client.
 
--
Peter

On Dec 23, 2007 2:02 AM, Cerebrus <zor...@sify.com> wrote:

My point exactly ! But all too often, users neglect to even set their
"regional options", which would mean that everything is "en-US" by
default. Also, that is only valid for Windows apps. Since the OP
mentioned "cs Page script ", I assumed it was a Web app, in which
case, I reiterate, you would need some kinda hint towards CultureInfo.

Marra

unread,
Dec 24, 2007, 3:06:27 AM12/24/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You could force the culture using drop down menus for DOM and Month
and year.

Nico VanHaaster

unread,
Dec 22, 2007, 9:56:41 PM12/22/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I appreciate all your posts, there are some interesting information in
there.

By cultureInfo you mean by the way that the user inputs dates wether
it be dd/mm/yyyy and or mm/dd/yyyy etc. There isn't a need currently
for international input as its a intranet based CRM system.

Alexander Higgins

unread,
Dec 24, 2007, 9:39:08 AM12/24/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
right on...
> > what did people do before microsoft wrote the code for them ????????- Hide quoted text -
>
> - Show quoted text -

Alexander Higgins

unread,
Dec 24, 2007, 9:40:52 AM12/24/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
And and in is such a case, simply create an overload of the method
allowing the culture info to be tested.


On Dec 22, 7:36 am, "Andrew Badera" <and...@badera.us> wrote:
> It's going to default to your main thread's culture to begin with. The only
> time you need to worry about setting culture info is when the application is
> executing on a machine whose default system culture is not what the user
> wants to utilize. Now sure, if there are localization or
> internationalization concerns, absolutely set your culture, but otherwise,
> run as client default.
>
> On 12/22/07, Cerebrus <zorg...@sify.com> wrote:
>
>
>
>
>
> > Yes, but how would the framework determine what a given user means by
> > (as in my example) a string such as "10/13/2007", if you don't give it
> > a hint about the culture ?
>
> > On Dec 21, 3:45pm, "Andrew Badera" <and...@badera.us> wrote:
> > > The OP has already half-implemented TryParse as it is ...
>
> > > Cerebrus -- why do you need to know culture? Let the framework handle
> > it.
> > > You don't need to know a thing about it -- TryParse is the way to go,
> > > culture is under the hood.
>
> > > --Andrew Badera- Hide quoted text -

har...@gmail.com

unread,
Dec 25, 2007, 10:33:03 AM12/25/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I second peter's opinion. A hint of the accepted format on the date
Textbox will make your life much easier. Then you can use the TryParse
method.

Regards,
Hardono


On Dec 24, 10:40 pm, Alexander Higgins <alexhiggins...@hotmail.com>
wrote:

Alexander Higgins

unread,
Dec 27, 2007, 7:22:27 PM12/27/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
A microcontroller project eh....

I recently did one to shoot my paintball gun using a laser sensor..
The video of how I made is in the articles section of http://www.teamcynergy.com

Charles A. Lopez

unread,
Dec 27, 2007, 9:47:42 PM12/27/07
to DotNetDe...@googlegroups.com
just thinking about this problem..
 
a good way (for me at least) is to create a calendar object that only allows you to select valid dates.

User input is prone to error.
 
Hey Y2K kept many people employed for a while didn't it?
 
 
 

Bob Heitzman

unread,
Dec 28, 2007, 5:48:41 PM12/28/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I think your ConvertTo method is fine, especially if this is client
side code or it will never have to scale up to 1,000's of executions a
second. (Assuming the C# code is OK in general, I'm a VB programmer
and we have IsDate().)
Reply all
Reply to author
Forward
0 new messages