473,378 Members | 1,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Date validation

Hi all a quick question: how can I validate a date in asp.net (2.0) with c#?
I didn't find a quick anwer.
Thanks, Diego.
Nov 17 '05 #1
11 8098
Hi Diego

Do you mean Client, or server side validation ?

kind regards

Ronnie

"Diego" wrote:
Hi all a quick question: how can I validate a date in asp.net (2.0) with c#?
I didn't find a quick anwer.
Thanks, Diego.

Nov 17 '05 #2
Possibly both :)
Thanks, Diego
"Ronnie Edgar" <Ronnie Ed***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
Hi Diego

Do you mean Client, or server side validation ?

kind regards

Ronnie

"Diego" wrote:
Hi all a quick question: how can I validate a date in asp.net (2.0) with
c#?
I didn't find a quick anwer.
Thanks, Diego.

Nov 17 '05 #3
Use a custom validator for client side validation. with a datetime
validation script as the validationscript.

and server side

something like

try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}

"Diego" wrote:
Possibly both :)
Thanks, Diego
"Ronnie Edgar" <Ronnie Ed***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
Hi Diego

Do you mean Client, or server side validation ?

kind regards

Ronnie

"Diego" wrote:
Hi all a quick question: how can I validate a date in asp.net (2.0) with
c#?
I didn't find a quick anwer.
Thanks, Diego.


Nov 17 '05 #4

This option isn't the correct way because if the date is not valid occurs an
exception an spend a lot of time.

You must verify if the date is valid, manually, in other words you have to
validate the format and you have to verify if the day is correct in the
respectly month and year.

I have the same problem as you and the exception way doesn't work well!

"Ronnie Edgar" <Ro*********@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...
Use a custom validator for client side validation. with a datetime
validation script as the validationscript.

and server side

something like

try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}

"Diego" wrote:
Possibly both :)
Thanks, Diego
"Ronnie Edgar" <Ronnie Ed***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
> Hi Diego
>
> Do you mean Client, or server side validation ?
>
> kind regards
>
> Ronnie
>
> "Diego" wrote:
>
>> Hi all a quick question: how can I validate a date in asp.net (2.0)
>> with
>> c#?
>> I didn't find a quick anwer.
>> Thanks, Diego.
>>
>>
>>



Nov 17 '05 #5
KH
In .NET 2.0 you can use DateTime.TryParse() rather than having to handle the
exception.
"Rodrigo Ferreira" wrote:

This option isn't the correct way because if the date is not valid occurs an
exception an spend a lot of time.

You must verify if the date is valid, manually, in other words you have to
validate the format and you have to verify if the day is correct in the
respectly month and year.

I have the same problem as you and the exception way doesn't work well!

"Ronnie Edgar" <Ro*********@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...
Use a custom validator for client side validation. with a datetime
validation script as the validationscript.

and server side

something like

try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}

"Diego" wrote:
Possibly both :)
Thanks, Diego
"Ronnie Edgar" <Ronnie Ed***@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
> Hi Diego
>
> Do you mean Client, or server side validation ?
>
> kind regards
>
> Ronnie
>
> "Diego" wrote:
>
>> Hi all a quick question: how can I validate a date in asp.net (2.0)
>> with
>> c#?
>> I didn't find a quick anwer.
>> Thanks, Diego.
>>
>>
>>


Nov 17 '05 #6
Rodrigo Ferreira wrote:

This option isn't the correct way because if the date is not valid occurs an
exception an spend a lot of time.
Define "a lot of time".
I think it was Jon Skeet who did a test a while back in a thread and
threw 10,000 odd exceptions a second.
Exception blocks like the below do have their place, maybe not in this
(although it would do the job just fine) case but "a lot of time" is a
very subjective phrase.

To the OP.
There are numerous date validation regexes out there which should do the
job just fine.
Try <http://www.regexlib.com>
for a couple of examples.

HTH
JB

You must verify if the date is valid, manually, in other words you have to
validate the format and you have to verify if the day is correct in the
respectly month and year.

I have the same problem as you and the exception way doesn't work well!

"Ronnie Edgar" <Ro*********@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...
Use a custom validator for client side validation. with a datetime
validation script as the validationscript.

and server side

something like

try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}

"Diego" wrote:

Possibly both :)
Thanks, Diego
"Ronnie Edgar" <Ronnie Ed***@discussions.microsoft.com> wrote in message
news:AA**********************************@micro soft.com...

Hi Diego

Do you mean Client, or server side validation ?

kind regards

Ronnie

"Diego" wrote:
>Hi all a quick question: how can I validate a date in asp.net (2.0)
>with
>c#?
>I didn't find a quick anwer.
>Thanks, Diego.
>
>
>


Nov 17 '05 #7
John B <jb******@yahoo.com> wrote:
This option isn't the correct way because if the date is not valid occurs an
exception an spend a lot of time.
Define "a lot of time".
I think it was Jon Skeet who did a test a while back in a thread and
threw 10,000 odd exceptions a second.


Far more than that - about 90,000 I think.
Exception blocks like the below do have their place, maybe not in this
(although it would do the job just fine) case but "a lot of time" is a
very subjective phrase.


Indeed :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Rodrigo Ferreira <rj*********@gmail.com> wrote:
This option isn't the correct way because if the date is not valid occurs an
exception an spend a lot of time.
Nonsense - you spend in the order of about 100th of a millisecond on a
reasonable processor in release mode.
You must verify if the date is valid, manually, in other words you have to
validate the format and you have to verify if the day is correct in the
respectly month and year.

I have the same problem as you and the exception way doesn't work well!


Does that mean you've performed tests of this *in release mode* and
have performance statistics? I'd be fascinated to see those statistics
- do you have that many users that you'll be throwing out tens of
thousands of invalid dates every second?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
Ronnie Edgar <Ro*********@discussions.microsoft.com> wrote:
Use a custom validator for client side validation. with a datetime
validation script as the validationscript.

and server side

something like

try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}


There's no point in catching the exception if you're just going to
throw it again - just let it bubble up if that's the behaviour you
want. If you *don't* want to throw an exception, then catch it (but
preferrably only specific ones rather than just plain Exception).

If you don't want to throw an exception, then as of .NET 2.0, you can
use DateTime.TryParse, which would be preferrable to just catching the
exception. (The exception will be a slight performance penalty - not
nearly the bugbear that it's commonly made out to be, but using
TryParse avoids it without you having to write any custom validation
code.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
fair point

"Jon Skeet [C# MVP]" wrote:
try
{
datetime.parse (myVariable);
}
catch (exception ex)
{
throw;
}


There's no point in catching the exception if you're just going to
throw it again - just let it bubble up if that's the behaviour you
want. If you *don't* want to throw an exception, then catch it (but
preferrably only specific ones rather than just plain Exception).

If you don't want to throw an exception, then as of .NET 2.0, you can
use DateTime.TryParse, which would be preferrable to just catching the
exception. (The exception will be a slight performance penalty - not
nearly the bugbear that it's commonly made out to be, but using
TryParse avoids it without you having to write any custom validation
code.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #11
In message <MP************************@msnews.microsoft.com >, Jon Skeet
<?@pobox.com.invalid> writes
If you don't want to throw an exception, then as of .NET 2.0, you can
use DateTime.TryParse, which would be preferrable to just catching the
exception. (The exception will be a slight performance penalty - not
nearly the bugbear that it's commonly made out to be, but using
TryParse avoids it without you having to write any custom validation
code.)


Just out of interest, has anyone looked at the IL for TryParse?

--
Steve Walker
Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
14
by: Steve Wright | last post by:
I am trying to validate if an entry on a form is a date. I have adapted code I found here http://www.codingforums.com/archive/index.php/t-14325 as below but I can't seem the get the results that I...
3
by: LDD | last post by:
Are there any built in time and/or date validation methods that one can make use of in .NET? thanks folks LDD
3
by: pmarisole | last post by:
The following javascript code gives me the date validation that I need except after the correct date is entered into the field, it puts the date in the wrong format EXAMPLE: User enters...
2
by: John Smith | last post by:
Hello, I have a VB.NET application with a Windows form that have several textboxes fields where I have dates entered. I would like to do a date validation check after the the field is updated, so...
1
by: jsredhar | last post by:
Dear Experts, I have two text boxes in my form (f1) i'm prompting the users to select the date from callender which is embeded within the from. when the users Pick the date from the calender.it...
8
pradeepjain
by: pradeepjain | last post by:
<script language = "Javascript"> /** * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/) */ // Declaring valid date character, minimum year and maximum...
5
Stang02GT
by: Stang02GT | last post by:
I have been asked to validate a date on our web-page so that people cannot enter dates like 14/1/08 or 2/30/06. I have found code that will do exactly what i need it to do, but i am not sure how to...
2
by: mshroom12 | last post by:
I am having trouble with the following project on hand. I use Eclipse to do my work in Java. This is what I'm supposed to complete. Date Validation In this exercise you will write a program...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.