Re: Date Validation for 3 Dropdowns!?
JRS: In article <d4l1n2$f0o$1@h ercules.btinter net.com>, dated Tue, 26
Apr 2005 09:30:10, seen in news:comp.lang. javascript, Ash <spam_ash@a-
hall.com> posted :[color=blue]
>
>"Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
>news:wtPgnJA4C UbCFwuD@merlyn. demon.co.uk...[color=green]
>> JRS: In article <mo4be.19156$G8 .5402@text.news .blueyonder.co. uk>, dated
>> Mon, 25 Apr 2005 11:20:50, seen in news:comp.lang. javascript, Michael
>> Winter <m.winter@bluey onder.co.invali d> posted :[color=darkred]
>>>Ash wrote:[/color][/color]
>
>
>Thanks for your incredibly detailed reply, my understanding of other
>logical languages is fairly good, but its the actual implementation of any
>javascript that my ability to get the correct syntax seems to fail - I will
>give it a go, but I'm sure I'll fail in actually amending my existing page
>to incorporate this code.
>
>In the current script, you'll notice a function...
>
> function validateFields( ){
> return true;
> }
>
>To keep things simple, I would like to insert a code block into this
>function that will simply...
>
> //1. Arrival date is at least today (you may have more
>restrictions )
> //2. Departure date is after arrival
> //3. Arrival and Departure dates are valid dates.
>
>I feel real guilty asking for this, but would it be possible to construct
>the actual code that I could dump straight into this function?
>
>Really appreciate your help, at least from there I can see how the syntax
>works in the context, and perhaps learn something for the future, hehe![/color]
Addressing MW : if validating date drop-downs, the year and month will
be plausible, so one should only need (UNTESTED)
function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
return new Date(y, m, d).getDate() == d /* was y, m */ }
FFF = Fred Flintstone Format - M/D/Y.
Addressing Ash - here's a start, lightly tested as mentioned :
AY = "2003" ; AM = "02" ; AD = "26" // Arr
DY = "2009" ; DM = "04" ; DD = "28" // Dep
function InvalidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
with (D=new Date(y, m, d))
return (getMonth()==m && getDate()==d) ? D : NaN /* was y, m */ }
if (!(Arr = InvalidDate(AY, AM-1, AD))) alert('Bad Arr')
if (!(Dep = InvalidDate(DY, DM-1, DD))) alert('Bad Dep')
if (Arr >= Dep) alert('Bad stay')
if (Arr < new Date()) alert('Too soon')
Stay = Math.round((Dep-Arr)/864e5)
There may be a micro-bug if the code is run at exactly midnight.
If you have a TARDIS, do not use the above code for bookings starting or
ending on 1st Jan 1970, in locations where the OS thinks the clock was
then GMT. But here, it wasn't; we had British Standard Time then ( as
BST but all year). But MS does not know that.
Maybe
return (getMonth()==m && getDate()==d) ? D : NaN /* was y, m */ }
InvalidDate needs renaming.
Only the first alert, if more than one, is necessarily valid.
If it is necessary to force D = new Date() [which is 'NOW'] to the
beginning of today or tomorrow, use D.setHours(X, 0, 0, 0) with X=0
or X=24.
NOTE : that allows a booking for Apr 27 to be made as late as Apr 26
23:59:59.99.
NOTE : remember that American clocks are generally 5-10 hours slow, and
Australian ones nearly half a day fast - if you allow foreign visitors.
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
JRS: In article <d4l1n2$f0o$1@h ercules.btinter net.com>, dated Tue, 26
Apr 2005 09:30:10, seen in news:comp.lang. javascript, Ash <spam_ash@a-
hall.com> posted :[color=blue]
>
>"Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
>news:wtPgnJA4C UbCFwuD@merlyn. demon.co.uk...[color=green]
>> JRS: In article <mo4be.19156$G8 .5402@text.news .blueyonder.co. uk>, dated
>> Mon, 25 Apr 2005 11:20:50, seen in news:comp.lang. javascript, Michael
>> Winter <m.winter@bluey onder.co.invali d> posted :[color=darkred]
>>>Ash wrote:[/color][/color]
>
>
>Thanks for your incredibly detailed reply, my understanding of other
>logical languages is fairly good, but its the actual implementation of any
>javascript that my ability to get the correct syntax seems to fail - I will
>give it a go, but I'm sure I'll fail in actually amending my existing page
>to incorporate this code.
>
>In the current script, you'll notice a function...
>
> function validateFields( ){
> return true;
> }
>
>To keep things simple, I would like to insert a code block into this
>function that will simply...
>
> //1. Arrival date is at least today (you may have more
>restrictions )
> //2. Departure date is after arrival
> //3. Arrival and Departure dates are valid dates.
>
>I feel real guilty asking for this, but would it be possible to construct
>the actual code that I could dump straight into this function?
>
>Really appreciate your help, at least from there I can see how the syntax
>works in the context, and perhaps learn something for the future, hehe![/color]
Addressing MW : if validating date drop-downs, the year and month will
be plausible, so one should only need (UNTESTED)
function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
return new Date(y, m, d).getDate() == d /* was y, m */ }
FFF = Fred Flintstone Format - M/D/Y.
Addressing Ash - here's a start, lightly tested as mentioned :
AY = "2003" ; AM = "02" ; AD = "26" // Arr
DY = "2009" ; DM = "04" ; DD = "28" // Dep
function InvalidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
with (D=new Date(y, m, d))
return (getMonth()==m && getDate()==d) ? D : NaN /* was y, m */ }
if (!(Arr = InvalidDate(AY, AM-1, AD))) alert('Bad Arr')
if (!(Dep = InvalidDate(DY, DM-1, DD))) alert('Bad Dep')
if (Arr >= Dep) alert('Bad stay')
if (Arr < new Date()) alert('Too soon')
Stay = Math.round((Dep-Arr)/864e5)
There may be a micro-bug if the code is run at exactly midnight.
If you have a TARDIS, do not use the above code for bookings starting or
ending on 1st Jan 1970, in locations where the OS thinks the clock was
then GMT. But here, it wasn't; we had British Standard Time then ( as
BST but all year). But MS does not know that.
Maybe
return (getMonth()==m && getDate()==d) ? D : NaN /* was y, m */ }
InvalidDate needs renaming.
Only the first alert, if more than one, is necessarily valid.
If it is necessary to force D = new Date() [which is 'NOW'] to the
beginning of today or tomorrow, use D.setHours(X, 0, 0, 0) with X=0
or X=24.
NOTE : that allows a booking for Apr 27 to be made as late as Apr 26
23:59:59.99.
NOTE : remember that American clocks are generally 5-10 hours slow, and
Australian ones nearly half a day fast - if you allow foreign visitors.
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Comment