Author |
Topic |
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2006-06-06 : 12:41:53
|
I have a text field in my app that the user is required to put a date of birth into. On the same page is a submit button that causes validation of the data entered onto the page using a function I wrote. When it gets to the date entered, if it's invalid I get an unhandled exception (stack overflow)The code concerned is Dim dtDob As New Date Try dtDob = CDate(Me.txtDoB.Text) If dtDob < CDate("01/01/1900") Or dtDob > Today() Then ValidateNewData = False End If Catch ex As Exception ValidateNewData = False End Try The line that throws the error is the "dtDob = CDate(Me.txtDoB.Text)" I thought that this error (i.e a daft date of birth) would throw a (handled) exception. What am I missing? Am I going to have to use a calendar control? If so does anyone know of any good (free) ones?many thankssteve-----------Oh, so they have internet on computers now! |
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2006-06-06 : 13:20:04
|
That code as writen should hit the CATCH().Mayb try this instead:dtDob = CTYPE(Me.txtDoB.Text, Date) That may work better in this case.Why not add a regular expression validator to txtDoB?That would allow you to validate the DOB on the client side before it gets to the server, so that your CDATE will not fail.Michael<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>Opinions expressed in this post are not necessarily those of TeleVox Software, inc. All information is provided "AS IS" with no warranties and confers no rights. |
 |
|
dfiala
Posting Yak Master
116 Posts |
Posted - 2006-06-06 : 13:27:06
|
I'd stay away from the old VB functions and try this..Dim DOB as DateTimeif Not DateTime.TryParse(Me.txtDoB.Text, DOB) then ValidateNewData = FalseElse if DOB < DateTime.Parse("1/1/1900") or DOB > DateTime.Today Then ValidateNewData = FalseEnd ifDean FialaVery Practical Software, IncNow with Blogging...http://www.vpsw.com/blogbabyMicrosoft MVP |
 |
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2006-06-06 : 14:31:31
|
Why not use 3 drop down selectors. One for month day and year. Then you can build the correct datetime variable out of that.If you give a user just a text box they could enter anything. "3/6/98", 3/6/1998", 6/3/98, 6-3-98, June 1st 1981, etc... You would then have to interpret the input which is next to impossible.Just my 2 pennies...JBelthoff• Hosts Station is a Professional Asp Hosting Provider• Position SEO can provide your company with SEO Services at an affordable price› As far as myself... I do this for fun! |
 |
|
dfiala
Posting Yak Master
116 Posts |
Posted - 2006-06-06 : 14:50:27
|
Lot of work. You'd want to make the dropdown for the day conditional based on the month and year, or you could still get invalid dates. April 31, February 29, 2003. It is also clunky looking from a user perspective. Best bet when working with dates is to either invest in a custom control that handles all the shenanigans for you, or simply try and parse what the user sends you. If it's a valid date, it's date. If not, you display an error.Dean FialaVery Practical Software, IncNow with Blogging...http://www.vpsw.com/blogbabyMicrosoft MVP |
 |
|
JBelthoff
Posting Yak Master
173 Posts |
Posted - 2006-06-06 : 15:07:44
|
quote: If it's a valid date, it's date.
"3/6/98" is a valid date. But is it March 6 or June 3?JBelthoff• Hosts Station is a Professional Asp Hosting Provider• Position SEO can provide your company with SEO Services at an affordable price› As far as myself... I do this for fun! |
 |
|
dfiala
Posting Yak Master
116 Posts |
Posted - 2006-06-06 : 15:08:47
|
depends on your cultureDean FialaVery Practical Software, IncNow with Blogging...http://www.vpsw.com/blogbabyMicrosoft MVP |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-06-06 : 21:57:08
|
quote: Originally posted by dfiala I'd stay away from the old VB functions and try this..Dim DOB as DateTimeif Not DateTime.TryParse(Me.txtDoB.Text, DOB) then ValidateNewData = FalseElse if DOB < DateTime.Parse("1/1/1900") or DOB > DateTime.Today Then ValidateNewData = FalseEnd ifDean FialaVery Practical Software, IncNow with Blogging...http://www.vpsw.com/blogbabyMicrosoft MVP
FYI -- I'm 100% not sure, but I think TryParse() isn't available unless you are using .NET 2.0. |
 |
|
dfiala
Posting Yak Master
116 Posts |
Posted - 2006-06-06 : 22:05:33
|
You are correct.In 1.1 you need to do the try yourself...Dim DOB as DateTimeTry DOB = DateTime.Parse(Me.txtDoB.Text)if DOB < DateTime.Parse("1/1/1900") or DOB > DateTime.Today ThenValidateNewData = FalseEnd ifCatch Ex as Exception ValidateNewData = FalseEnd TryDean FialaVery Practical Software, IncNow with Blogging...http://www.vpsw.com/blogbabyMicrosoft MVP |
 |
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2006-06-07 : 08:29:33
|
Thanks guys for all the advice, much appreciated. I should have said I am using .NET 1.1. Ideally I want something very quick and easy - so for me at least regular expressions are out as I've never really got to grips with them.thankssteve-----------Oh, so they have internet on computers now! |
 |
|
dfiala
Posting Yak Master
116 Posts |
Posted - 2006-06-07 : 08:40:28
|
Regex for full date validation gets exceptionally long in order to handle leap years properly. My previous post showed the simplest way to handle server-side validation in 1.1.If you want to do client-side check out: http://www.smartwebby.com/DHTML/date_validation.aspDean FialaVery Practical Software, IncNow with Blogging...http://www.vpsw.com/blogbabyMicrosoft MVP |
 |
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2006-06-08 : 10:50:02
|
I've decided to create my own controlIt has 2 drop downs for month and year, and a calendar bit for selecting the date and it can dissappear into an icon. If nothing else it was a useful learning experience creating it.many thanks to all for your ideas and suggestionssteve-----------Oh, so they have internet on computers now! |
 |
|
|