html form: date field auto-adjust

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Kruse

    #16
    Re: html form: date field auto-adjust

    Martin Herrman wrote:[color=blue]
    > I am working on a HTML form in which a date must be entered of the
    > form 'dd-mm-yyyy'. Now I'm looking for a script that, when the user
    > switches to another form field, changes e.g. 'ddmmyyyy' or 'dd-m-yy'
    > to 'dd-mm-yyyy'.[/color]

    Using my functions at: http://www.mattkruse.com/javascript/date/

    function switchDate(obj) {
    var d = parseDate(obj.v alue);
    if (d==null) {
    obj.value = "";
    return;
    }
    obj.value = formatDate(d,"d d-MM-yyyy");
    }

    <input type="text" name="date" onChange="switc hDate(this)">

    // ------------------------------------------------------------------
    // parseDate( date_string [, prefer_euro_for mat] )
    //
    // This function takes a date string and tries to match it to a
    // number of possible date formats to get the value. It will try to
    // match against the following international formats, in this order:
    // y-M-d MMM d, y MMM d,y y-MMM-d d-MMM-y MMM d
    // M/d/y M-d-y M.d.y MMM-d M/d M-d
    // d/M/y d-M-y d.M.y d-MMM d/M d-M
    // A second argument may be passed to instruct the method to search
    // for formats like d/M/y (european format) before M/d/y (American).
    // Returns a Date object or null if no patterns match.
    // ------------------------------------------------------------------

    Disclaimers:

    (1) Be aware that users may enter dates in formats that you aren't
    expecting, and may be surprised at the results they see.

    (2) Always validate on the server-side if your code depends on a certain
    format.

    --
    Matt Kruse



    Comment

    • Lee

      #17
      Re: html form: date field auto-adjust

      Matt Kruse said:[color=blue]
      >
      >Lee wrote:[color=green]
      >> It's not a reasonable request. Just because the OP believes
      >> that every user will choose one of those three formats, why
      >> in the world should you go along with his fantasy?[/color]
      >
      >It is just as unreasonable to assume that you know and understand every
      >situation that could exist. Enough so to state that the original request is
      >unreasonable .
      >
      >A reasonable response would be to answer the question, then point out the
      >possible problems with such a situation. The OP can consider them if
      >necessary, or decide that they don't apply.[/color]

      "Here's your answer, but it won't work."

      I'll leave that to others, thanks.

      Comment

      • Matt Kruse

        #18
        Re: html form: date field auto-adjust

        Lee wrote:[color=blue]
        > "Here's your answer, but it won't work."
        > I'll leave that to others, thanks.[/color]

        The point is, you don't know it won't work. Because you don't know the
        entire situation.

        Furthermore, not every solution needs to be 100% fool-proof to provide
        value.

        If someone types 1/2/2005 into the OP's form and it's changed to 02-01-2005
        when they really meant 01-02-2005, then the user will see that the date is
        wrong and correct it.

        Or, maybe the user wants this for an intranet in a single country where
        everyone will type in dates in the same format. Or maybe a web site that
        only serves a local community, where everyone will enter dates in the same
        format.

        You never know.

        --
        Matt Kruse



        Comment

        • Dr John Stockton

          #19
          Re: html form: date field auto-adjust

          JRS: In article <d183vb02cd6@ne ws4.newsguy.com >, dated Tue, 15 Mar 2005
          20:00:09, seen in news:comp.lang. javascript, Matt Kruse
          <newsgroups@mat tkruse.com> posted :[color=blue]
          >Martin Herrman wrote:[/color]
          [color=blue]
          >// This function takes a date string and tries to match it to a
          >// number of possible date formats to get the value. It will try to
          >// match against the following international formats, in this order:[/color]

          Unfortunately, the Americans think that the word "foreign" is spelt
          i n t e r n a t i o n a l .

          The international formats are yyyy-mm-dd and certain others derived from
          that, such as yyyymmdd; they are given by the internationally-agreed
          standard ISO 8601.

          Those that followed were multi-national formats.

          There is always a risk with code that is willing to accept either, say,
          25/12/2004 or 12/25/2004 may be employed without testing that it gives
          the right result for days before the 13th of the month with D!=M.
          [color=blue]
          >(2) Always validate on the server-side if your code depends on a certain
          >format.[/color]

          Never make rash assumptions; there may not *be* server side processing;
          there may be nothing sent from the reader's copy of a Web page back over
          the Internet, or the javascript may be executed by WSH on the machine
          where it resides.

          Final validation should be on a machine under the control of those who
          use the values in question. Validation at data entry may well be
          helpful to those who enter data. That, I think, covers everything.

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

          • Matt Kruse

            #20
            Re: html form: date field auto-adjust

            Dr John Stockton wrote:[color=blue][color=green]
            >> // This function takes a date string and tries to match it to a
            >> // number of possible date formats to get the value. It will try to
            >> // match against the following international formats, in this order:[/color]
            > Unfortunately, the Americans think that the word "foreign" is spelt
            > i n t e r n a t i o n a l .
            > The international formats are yyyy-mm-dd and certain others derived
            > from that, such as yyyymmdd; they are given by the
            > internationally-agreed standard ISO 8601.[/color]

            International means:
            1. Of, relating to, or involving two or more nations
            2. Extending across or transcending national boundaries

            A list of "internatio nal" formats, to me, means those date formats used
            across various nations.

            But if you want to play semantics games, go ahead...

            --
            Matt Kruse



            Comment

            • rh

              #21
              Re: html form: date field auto-adjust

              Dr John Stockton wrote:

              <..>
              [color=blue]
              > function FudgeDate(S) { var A, m = "-", O = '0'
              > if (/^(\d\d)(\d\d)(\ d\d\d\d)$/.test(S))
              > with (RegExp) return $1 + m + $2 + m + $3
              > if (/^(\d)(\d)(\d\d) $/.test(S)) // ?
              > with (RegExp) return O + $1 + m + O + $2 + m + "20" + $3 // ?
              > A = S.split(/\D+/)
              > if (A.length!=3) return
              > if (+A[2]<100) A[2] = +A[2] + 2000
              > if (A[1].length<2) A[1] = O + A[1]
              > if (A[0].length<2) A[0] = O + A[0]
              > return A[0] + m + A[1] + m + A[2] }
              >
              >
              > There's something better than using test & $1 $2 $3 that way; but I
              > don't recall it at the moment.
              >[/color]

              You may have been thinking of "match" where the captures are available
              in the resulting array (provided the "g" modifier is not used), e.g.:

              function FudgeDate( v ) {
              var x;
              v = v.replace( /^\s+|\s+$/g, "" );
              if ( x = v.match( /^(\d\d)(\d\d)(\ d\d\d\d)$/ ) );
              else if( x = v.match( /^(\d)(\d)(\d\d) $/ ) );
              if ( x ) x.shift( );
              else {
              x = v.split( /\D+/ );
              if ( x.length < 3 ) return;
              }
              if ( x[ 2 ] < 100 ) x[ 2 ] = + x[ 2 ] + 2000;
              return ( "0"+x.join( "-0" ) ).replace( /0(\d{2})/g, "$1" );
              }


              As some are "shift" deficient:

              Array.prototype .shift = ( function ( ) {
              if ( Array.prototype .shift ) return Array.prototype .shift;
              return function( ) {
              var ret;
              if( this.length ) {
              ret = this[ 0 ];
              for ( var k = 1; k < this.length; k++ ) this[ k-1 ] = this[
              k ];
              this.length--;
              }
              return ret;
              }
              }
              ) ( );

              ../rh

              Comment

              • Lee

                #22
                Re: html form: date field auto-adjust

                Matt Kruse said:[color=blue]
                >
                >Lee wrote:[color=green]
                >> It seems to me to be more prudent and efficient to point out
                >> serious problems before wasting time developing a solution.[/color]
                >
                >My response took about 1 minute to post, and it seems to address the
                >poster's request perfectly.
                >I even added some disclaimers.
                >And I wasn't an ass about it.[/color]

                So you've sent the OP off to implement your solution with
                the thought, somewhere in the back of his mind, that it may
                not work all the time, and that there is something about doing
                it on the server that might be important, too. Congratulations
                on not being an ass.

                Comment

                • Lasse Reichstein Nielsen

                  #23
                  Re: html form: date field auto-adjust

                  "Matt Kruse" <newsgroups@mat tkruse.com> writes:
                  [color=blue]
                  > International means:
                  > 1. Of, relating to, or involving two or more nations
                  > 2. Extending across or transcending national boundaries
                  >
                  > A list of "internatio nal" formats, to me, means those date formats used
                  > across various nations.[/color]

                  I.e., any format used in more than one nation. I.e., any format :)

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Dr John Stockton

                    #24
                    Re: html form: date field auto-adjust

                    JRS: In article <d1auvu02o46@ne ws4.newsguy.com >, dated Wed, 16 Mar 2005
                    21:53:32, seen in news:comp.lang. javascript, Matt Kruse
                    <newsgroups@mat tkruse.com> posted :[color=blue]
                    >Dr John Stockton wrote:[color=green][color=darkred]
                    >>> // This function takes a date string and tries to match it to a
                    >>> // number of possible date formats to get the value. It will try to
                    >>> // match against the following international formats, in this order:[/color]
                    >> Unfortunately, the Americans think that the word "foreign" is spelt
                    >> i n t e r n a t i o n a l .
                    >> The international formats are yyyy-mm-dd and certain others derived
                    >> from that, such as yyyymmdd; they are given by the
                    >> internationally-agreed standard ISO 8601.[/color]
                    >
                    >Internationa l means:[/color]

                    Insert : In the American dialect
                    [color=blue]
                    >1. Of, relating to, or involving two or more nations
                    >2. Extending across or transcending national boundaries
                    >
                    >A list of "internatio nal" formats, to me, means those date formats used
                    >across various nations.[/color]

                    That's because you know American, not English.
                    [color=blue]
                    >But if you want to play semantics games, go ahead...[/color]

                    However, many here have been taught properly - probably almost all of
                    those educated in mainland Europe, and the Scots of course, and some of
                    those educated not-too-recently in England too - and it will be helpful
                    for them to realise that what you appear to mean is not what you
                    actually mean. The OP, apparently a student in the Netherlands, may
                    well be one such.

                    To see the standard use of the word "international" , see how it is used
                    in International Standards - if you can find copies in the USA.

                    Your meaning is AIUI that customary in the US computer industry.

                    --
                    © John Stockton, Surrey, UK. ???@merlyn.demo n.co.uk Turnpike v4.00 MIME. ©
                    Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
                    Check boilerplate spelling -- error is a public sign of incompetence.
                    Never fully trust an article from a poster who gives no full real name.

                    Comment

                    • Dr John Stockton

                      #25
                      Re: html form: date field auto-adjust

                      JRS: In article <1111036928.672 127.299530@z14g 2000cwz.googleg roups.com>
                      , dated Wed, 16 Mar 2005 21:22:08, seen in news:comp.lang. javascript, rh
                      <codeHanger@yah oo.ca> posted :
                      [color=blue][color=green]
                      >> There's something better than using test & $1 $2 $3 that way; but I
                      >> don't recall it at the moment.
                      >>[/color]
                      >
                      >You may have been thinking of "match" where the captures are available
                      >in the resulting array (provided the "g" modifier is not used)[/color]

                      In principle, yes (and that might be better) : but a search of my site
                      suggests I was actually thinking of "exec" as in


                      function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
                      with (new Date(y, m, d))
                      return (getMonth()==m && getDate()==d) /* was y, m */ }

                      function ReadISO8601date (Q) { var T // adaptable for other layouts
                      if ((T = /^(\d+)([-\/])(\d\d)(\2)(\d\ d)$/.exec(Q)) == null)
                      { return -2 } // bad format
                      for (var j=1; j<=5; j+=2) T[j] = +T[j] // some use needs numbers
                      if (!ValidDate(T[1], T[3]-1, T[5])) { return -1 } // bad value
                      return [ T[1], T[3], T[5] ] }


                      which latter reads and validates an ISO date (being a little liberal as
                      regards separators, and only after Year -1) and returns either a
                      negative error number or an array [Y, M, D].

                      A version which if successful returns the Date Object generated in
                      ValidDate else undefined or NaN might be worth having.

                      + + +

                      I now have parameterised javascript for all the sorts of week number
                      that I can think of which have Week 1 near January 1, not just the
                      International Standard one (Week 1 starts on a fixed day of a fixed
                      month, can be trivially derived from code for Inland Revenue weeks) -
                      see <URL:http://www.merlyn.demo n.co.uk/weekcalc.htm#NI C>.

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

                      • rh

                        #26
                        Re: html form: date field auto-adjust

                        Dr John Stockton wrote:[color=blue]
                        > JRS: In article[/color]
                        <1111036928.672 127.299530@z14g 2000cwz.googleg roups.com>[color=blue]
                        > , dated Wed, 16 Mar 2005 21:22:08, seen in news:comp.lang. javascript,[/color]
                        rh[color=blue]
                        > <codeHanger@yah oo.ca> posted :
                        >[color=green][color=darkred]
                        > >> There's something better than using test & $1 $2 $3 that way; but[/color][/color][/color]
                        I[color=blue][color=green][color=darkred]
                        > >> don't recall it at the moment.
                        > >>[/color]
                        > >
                        > >You may have been thinking of "match" where the captures are[/color][/color]
                        available[color=blue][color=green]
                        > >in the resulting array (provided the "g" modifier is not used)[/color]
                        >
                        > In principle, yes (and that might be better) : but a search of my[/color]
                        site[color=blue]
                        > suggests I was actually thinking of "exec" as in
                        >[/color]

                        Take your choice. According to ECMA 262/3 - 15.5.4.10, in the absence
                        of the "g" modifier on the RegExp, use of "exec" and "match" should
                        provide identical results.

                        If there are browser exceptions, I'm not aware.

                        <..>

                        ../rh

                        Comment

                        • Randy Webb

                          #27
                          Re: html form: date field auto-adjust

                          Dr John Stockton wrote:
                          [color=blue]
                          > JRS: In article <d1auvu02o46@ne ws4.newsguy.com >, dated Wed, 16 Mar 2005
                          > 21:53:32, seen in news:comp.lang. javascript, Matt Kruse
                          > <newsgroups@mat tkruse.com> posted :
                          >[color=green]
                          >>Dr John Stockton wrote:
                          >>[color=darkred]
                          >>>>// This function takes a date string and tries to match it to a
                          >>>>// number of possible date formats to get the value. It will try to
                          >>>>// match against the following international formats, in this order:
                          >>>
                          >>>Unfortunatel y, the Americans think that the word "foreign" is spelt
                          >>>i n t e r n a t i o n a l .
                          >>>The international formats are yyyy-mm-dd and certain others derived
                          >>>from that, such as yyyymmdd; they are given by the
                          >>>internationa lly-agreed standard ISO 8601.[/color]
                          >>
                          >>Internation al means:[/color]
                          >
                          >
                          > Insert : In the American dialect[/color]

                          Insert: In the opinion of John Stockton, that is what it means "In the
                          American dialect".

                          If you were as educated on the dialects as you seem to think you are
                          then you would be well aware that there are at minimum 5 distinct
                          dialects in the USA.
                          [color=blue]
                          >[color=green]
                          >>1. Of, relating to, or involving two or more nations
                          >>2. Extending across or transcending national boundaries
                          >>
                          >>A list of "internatio nal" formats, to me, means those date formats used
                          >>across various nations.[/color]
                          >
                          >
                          > That's because you know American, not English.[/color]

                          Hmmm. Official Language of the USA: English.
                          [color=blue]
                          >[color=green]
                          >>But if you want to play semantics games, go ahead...[/color]
                          >
                          >
                          > However, many here have been taught properly[/color]

                          Are you that ignorant to imply that those taught in the American schools
                          were not "taught properly"? They *were* taught properly, they just
                          weren't taught what you think they should have been taught.


                          - probably almost all of[color=blue]
                          > those educated in mainland Europe, and the Scots of course, and some of
                          > those educated not-too-recently in England too - and it will be helpful
                          > for them to realise that what you appear to mean is not what you
                          > actually mean.[/color]

                          Insert: In the opinion of John Stockton.

                          But, how do you know what he meant to say? Are you a psychic Jackass now
                          or still just a Jackass?
                          [color=blue]
                          > To see the standard use of the word "international" , see how it is used
                          > in International Standards - if you can find copies in the USA.[/color]

                          The "standard" use is dependent on who you let define that standard. I
                          am unaware of *any* authority that gives a "standard" definition for
                          *any* word.
                          [color=blue]
                          > Your meaning is AIUI that customary in the US computer industry.[/color]

                          Among other places.

                          --
                          Randy
                          comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

                          Comment

                          • Dr John Stockton

                            #28
                            Re: html form: date field auto-adjust

                            JRS: In article <-fGdnYSDj_ECt6ff RVn-rQ@comcast.com> , dated Thu, 17 Mar
                            2005 20:11:00, seen in news:comp.lang. javascript, Randy Webb
                            <HikksNotAtHome @aol.com> posted :[color=blue]
                            >Dr John Stockton wrote:[/color]
                            [color=blue][color=green]
                            >> That's because you know American, not English.[/color]
                            >
                            >Hmmm. Official Language of the USA: English.[/color]

                            Only in the American language.
                            The proper term, in English, is American English.
                            There are resemblances; but there are definite differences, one of which
                            is the meaning of that word.

                            [color=blue]
                            >The "standard" use is dependent on who you let define that standard. I
                            >am unaware of *any* authority that gives a "standard" definition for
                            >*any* word.[/color]

                            Yes, most Americans are sadly ignorant of the Oxford English Dictionary
                            in particular, and of International Standards in general.

                            The undeniable point remains : that the way in which most Americans, or
                            at least most American technicians, use the word "Internatio nal" is
                            liable to deceive people in other parts of the world.

                            Evidently you now know the meaning of the word "Internatio nal" as used
                            in "Internatio nal Organization for Standards", often called
                            "Internatio nal Standards Organization" or just ISO : is there an
                            American word that expresses that meaning, as opposed to the US
                            customary one, or is their language deficient because Americans don't
                            have the concept?

                            --
                            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                            Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links;
                            Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
                            No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                            Comment

                            • John W. Kennedy

                              #29
                              Re: html form: date field auto-adjust

                              Dr John Stockton wrote:[color=blue]
                              > JRS: In article <-fGdnYSDj_ECt6ff RVn-rQ@comcast.com> , dated Thu, 17 Mar
                              > 2005 20:11:00, seen in news:comp.lang. javascript, Randy Webb
                              > <HikksNotAtHome @aol.com> posted :[color=green]
                              >>The "standard" use is dependent on who you let define that standard. I
                              >>am unaware of *any* authority that gives a "standard" definition for
                              >>*any* word.[/color][/color]
                              [color=blue]
                              > Yes, most Americans are sadly ignorant of the Oxford English Dictionary
                              > in particular, and of International Standards in general.[/color]

                              If you are under the curious impression that all the words of English
                              (or any other language, for that matter) are univocal, as you seem to be
                              pronouncing, I suggest you spend a little time with that magisterial
                              work, yourself. "Set" is instructive, but "cleave" is more amusing.

                              By the way, British educational standards are at least as debased as
                              American, these days; see
                              <URL:http://www.timesonline .co.uk/article/0,,1059-1530304,00.html > for a
                              grim example.

                              ---
                              John W. Kennedy
                              "You can, if you wish, class all science-fiction together; but it is
                              about as perceptive as classing the works of Ballantyne, Conrad and W.
                              W. Jacobs together as the 'sea-story' and then criticizing _that_."
                              -- C. S. Lewis. "An Experiment in Criticism"

                              Comment

                              • Douglas Crockford

                                #30
                                Re: html form: date field auto-adjust

                                >>The "standard" use is dependent on who you let define that standard. I[color=blue][color=green]
                                >>am unaware of *any* authority that gives a "standard" definition for
                                >>*any* word.[/color][/color]
                                [color=blue]
                                > Yes, most Americans are sadly ignorant of the Oxford English Dictionary
                                > in particular, and of International Standards in general.[/color]

                                This ain't France, bub. The OED is definitive on the evolution of the
                                language, but like most good dictionaries it is descriptive, not
                                prescriptive, when it comes to current usage.

                                'When _I_ use a word,' Humpty Dumpty said, in rather a scornful tone,
                                'it means just what I choose it to mean -- neither more nor less.'

                                'The question is,' said Alice, `whether you _can_ make words mean so
                                many different things.'

                                'The question is,' said Humpty Dumpty, 'which is to be master -- that's
                                all.'


                                Comment

                                Working...