Date Validation for 3 Dropdowns!?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr John Stockton

    #16
    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.

    Comment

    • Dr John Stockton

      #17
      Re: Date Validation for 3 Dropdowns!?

      JRS: In article <O1tbe.8126$XF3 .3938@twister.n yroc.rr.com>, dated Tue,
      26 Apr 2005 15:23:26, seen in news:comp.lang. javascript, Mick White
      <mwhite13BOGUS@ rochester.rr.co m> posted :[color=blue]
      >
      >function validateFields( ) {
      >var d=document.myFo rm
      > if (parseInt(d.ARR IVAL_DAY.value, 10) == 2 &&
      > d.ARRIVAL_MONTH .value > 28) {
      > alert('Invalid date');
      > return false;
      > }
      > return true;
      >}[/color]


      Funny calendar you have in your part of the USA - months after the 28th
      don't have a Day 2.

      Take a local copy of <URL:http://www.merlyn.demo n.co.uk/js-quick.htm> -
      DO NOT use the web copy repeatedly, for bandwidth reasons - and you can
      then easily test little bits of code before posting them.

      For dates in 2001-2099, and I guess that's enough for the OP, who should
      by then be promoted/sacked/retired, it should be sufficient to test that
      D <= 31 - (M==4 || M==6 || M==9 || M==11) and if M==2 that
      D <= 28 + (Y%4==0) .

      But the test with a Date Object will be better.


      GENERAL : a proportion of news posts (not only mine) uploaded to my ISP
      in the last week or so have been delayed, apparently before being
      offered to servers and peers; they are now appearing.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
      Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
      PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
      Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

      Comment

      • Mick White

        #18
        Re: Date Validation for 3 Dropdowns!?

        Dr John Stockton wrote:
        [color=blue]
        > <mwhite13BOGUS@ rochester.rr.co m> posted :[/color]
        [snip][color=blue][color=green]
        >> if (parseInt(d.ARR IVAL_DAY.value, 10) == 2 &&
        >> d.ARRIVAL_MONTH .value > 28) {[/color][/color]
        [snip][color=blue]
        >
        > Funny calendar you have in your part of the USA - months after the 28th
        > don't have a Day 2.[/color]

        Oops, perhaps I have the month and the date reversed...
        [color=blue]
        > Take a local copy of <URL:http://www.merlyn.demo n.co.uk/js-quick.htm> -
        > DO NOT use the web copy repeatedly, for bandwidth reasons - and you can
        > then easily test little bits of code before posting them.[/color]

        Mea culpa. I have some trouble with your site using Safari. The date
        object is, well, different.

        [color=blue]
        >
        > For dates in 2001-2099, and I guess that's enough for the OP, who should
        > by then be promoted/sacked/retired, it should be sufficient to test that
        > D <= 31 - (M==4 || M==6 || M==9 || M==11) and if M==2 that
        > D <= 28 + (Y%4==0) .
        >
        > But the test with a Date Object will be better.[/color]

        I agree since you are, at most, creating 2 date objects.
        Mick

        Comment

        • @sh

          #19
          Re: Date Validation for 3 Dropdowns!?

          > Oops, perhaps I have the month and the date reversed...
          [color=blue]
          > function isValidDate(dt, y, m, d) {
          > return (dt.getDate() == d) && (dt.getMonth() == m)
          > && (dt.getFullYear () == y);
          > }[/color]

          Just to say a massive thanks to all that contributed here, I really do wish
          I had the javascript understanding that you guys clearly have, its just the
          syntax that gets me, and the data subtypes, I hope to get there in the end.

          Its unfortunate that there are such within other threads so as to spoil the
          experience for Usenet newbies. I appreciate you must get some silly
          questions on here, but some must sit staring at their screen just waiting to
          post their negative picky shite.

          Thanks once again though for your help here, I'll stick around for a bit
          incase of further troubles ;o)

          p.s. it was my fault that the date/months were reversed...a typo I noticed
          shortly after, hehe!

          Cheers, Ash


          Comment

          • Dr John Stockton

            #20
            Re: Date Validation for 3 Dropdowns!?

            JRS: In article <1x8xe4kl.fsf@h otpop.com>, dated Tue, 26 Apr 2005
            21:11:22, seen in news:comp.lang. javascript, Lasse Reichstein Nielsen
            <lrn@hotpop.com > posted :[color=blue]
            >Michael Winter <m.winter@bluey onder.co.invali d> writes:
            >[color=green]
            >> Dr John Stockton wrote:[/color]
            >[color=green][color=darkred]
            >>> One does not need all three tests [...][/color]
            >>
            >> Perhaps not. I don't see any particular reason not to include them
            >> all, however it would probably be better to change the order of
            >> evaluation as the year is only likely to change in December (when
            >> input restrictions are imposed). It would potentially shorten the time
            >> necessary to discover invalid dates, but still add little overhead to
            >> valid ones.[/color]
            >
            >A good choice. It is a rare error that changes the year and not the
            >month. Indeed, most non-deliberate errors will be only a few days off
            >(e.g., 31st of April) and will error on both date and month.
            >
            >Moving the year check last will guarantee that it is only performed
            >for valid dates (but that goes for any of the checks if placed third).[/color]

            Omitting the year check allows the validation to be used for years given
            as 00..99 meaning 1900..1999, and for years 00..99 meaning 0000..0099 if
            an error for 0000-02-29 can be tolerated. It also avoids any doubt
            about the availability of getFullYear.

            [color=blue][color=green][color=darkred]
            >>> An OP posting via BT should surely not be using FFF order [...][/color]
            >>
            >> FFF?[/color]
            >
            >"Fred Flintstone Format", Dr. Stockton's own name for MM/DD/YY format.
            ><URL:http://www.merlyn.demo n.co.uk/datefmts.htm>[/color]

            No, not my own name, but one that I came across, possibly on the Web but
            more likely in News - maybe news:misc.metri c-system, and probably in
            conjunction with FFU, the system of measurement units that the Americans
            use and refer to as English (unfair to Welsh, Scots, and Irish) although
            the proper name in English is Imperial Units.

            --
            © 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

            • Dr John Stockton

              #21
              Re: Date Validation for 3 Dropdowns!?

              JRS: In article <L7Abe.8694$Bc7 .4504@twister.n yroc.rr.com>, dated Tue,
              26 Apr 2005 23:27:39, seen in news:comp.lang. javascript, Mick White
              <mwhite13BOGUS@ rochester.rr.co m> posted :
              [color=blue]
              > I have some trouble with your site using Safari. The date
              >object is, well, different.[/color]

              AIUI, Safari is at least moderately often used, though I know nothing of
              it myself, not even what it runs on. In that case, let's investigate :
              for example, how does it differ from ECMA-262, what on my site does not
              work, etc.? Can feature detection help? What should the FAQ say? Is
              there a Safari FAQ? a Safari javascript FAQ? a Safari javascript date
              FAQ?

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
              Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
              PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
              Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

              Comment

              • Mick White

                #22
                Re: Date Validation for 3 Dropdowns!?

                Dr John Stockton wrote:
                [color=blue]
                > JRS: In article <L7Abe.8694$Bc7 .4504@twister.n yroc.rr.com>, dated Tue,
                > 26 Apr 2005 23:27:39, seen in news:comp.lang. javascript, Mick White
                > <mwhite13BOGUS@ rochester.rr.co m> posted :
                >
                >[color=green]
                >>I have some trouble with your site using Safari. The date
                >>object is, well, different.[/color]
                >
                >
                > AIUI, Safari is at least moderately often used, though I know nothing of
                > it myself, not even what it runs on. In that case, let's investigate :
                > for example, how does it differ from ECMA-262, what on my site does not
                > work, etc.? Can feature detection help? What should the FAQ say? Is
                > there a Safari FAQ? a Safari javascript FAQ? a Safari javascript date
                > FAQ?
                >[/color]
                Safari will render only dates from ~1900 to ~2035.
                And it had trouble with "setting" the date

                function getWeekday(year ,weekday){
                var weekdays=new Array();
                var starter=getNthD ayInMonth(1,wee kday,"January", year);
                //1st monday of year

                var D=new Date("January "+starter+" , "+year).getTime ()
                for(var t=D;t<D+52*6048 e5;t+=6048e5){
                weekdays[weekdays.length]=new Date(t);
                }
                return weekdays;
                }

                In the loop, you'd think the following would work, but no.

                for(var t=0;t<52*7;t+=7 ){
                weekdays[weekdays.length]=D.setDate(t);
                }

                Likewise:
                var t=0,Y=D.getFull Year(),NY=D+1;
                while(year<NY){
                t+=7;
                weekdays[weekdays.length]=D.setDate(t)
                }
                Mick



                while(year <+1



                Comment

                • Mick White

                  #23
                  Re: Date Validation for 3 Dropdowns!?

                  Mick White wrote:
                  [color=blue]
                  > Dr John Stockton wrote:[/color]
                  [snip]
                  [color=blue]
                  > var D=new Date("January "+starter+" , "+year).getTime ();[/color]
                  [snip][color=blue]
                  > var t=0,Y=D.getFull Year(),NY=D+1;
                  > while(year<NY){
                  > t+=7;
                  > weekdays[weekdays.length]=D.setDate(t)
                  > }
                  > Mick[/color]

                  Forget the while clause , D is, of course, a timestamp, not a date object.

                  Mick

                  Comment

                  • Dr John Stockton

                    #24
                    Re: Date Validation for 3 Dropdowns!?

                    JRS: In article <fl4ce.11735$mG 3.6961@twister. nyroc.rr.com>, dated Thu,
                    28 Apr 2005 12:06:35, seen in news:comp.lang. javascript, Mick White
                    <mwhite13BOGUS@ rochester.rr.co m> posted :[color=blue]
                    >Dr John Stockton wrote:
                    >[color=green]
                    >> JRS: In article <L7Abe.8694$Bc7 .4504@twister.n yroc.rr.com>, dated Tue,
                    >> 26 Apr 2005 23:27:39, seen in news:comp.lang. javascript, Mick White
                    >> <mwhite13BOGUS@ rochester.rr.co m> posted :
                    >>
                    >>[color=darkred]
                    >>>I have some trouble with your site using Safari. The date
                    >>>object is, well, different.[/color]
                    >>
                    >>
                    >> AIUI, Safari is at least moderately often used, though I know nothing of
                    >> it myself, not even what it runs on. In that case, let's investigate :
                    >> for example, how does it differ from ECMA-262, what on my site does not
                    >> work, etc.? Can feature detection help? What should the FAQ say? Is
                    >> there a Safari FAQ? a Safari javascript FAQ? a Safari javascript date
                    >> FAQ?
                    >>[/color]
                    > Safari will render only dates from ~1900 to ~2035.[/color]

                    All Safari, or just some versions?

                    My js-dates.htm :
                    I hear that Safari 1.2.3 on Mac OS 10.3. shows a range of
                    +-2^31 seconds from zero - 1901-12-13 to 2038-01-19 GMT.

                    ECMA requires +-10^8 days. Safari's fault is grievous. In it, 30-year
                    look-ahead for US mortgages will fail in under 33 months from now. The
                    true range will probably be -2^31 to 2^31-1.

                    <URL:http://www.merlyn.demo n.co.uk/js-index.htm#SDB>, first yellow box,
                    should show that range, for Safari users; it determines the borders
                    outside which new Date(x) gives NaN.

                    [color=blue]
                    >And it had trouble with "setting" the date
                    > ...[/color]

                    Without knowing what you get, and what you expected, I can't really
                    comment on that. Remember that you (probably) see effects of Summer
                    Time.

                    --
                    © 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

                    Working...