html form: date field auto-adjust

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Herrman

    #1

    html form: date field auto-adjust

    Dear scripters,

    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'.

    I have searched the net using google, but only found validators that show
    an error if an incorrect format is used. (and I can't write one on my own)

    Does someone has such a script for me?

    much thanks in advance!

    Martin
  • Lee

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

    Martin Herrman said:[color=blue]
    >
    >Dear scripters,
    >
    >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'.
    >
    >I have searched the net using google, but only found validators that show
    >an error if an incorrect format is used. (and I can't write one on my own)[/color]

    That can't be done, because the code can't guess whether 1122005 is January 12
    or November 2. That's why you have to ask the user to correct it.

    Comment

    • McKirahan

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

      "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
      news:d17foe01ll 4@drn.newsguy.c om...[color=blue]
      > Martin Herrman said:[color=green]
      > >
      > >Dear scripters,
      > >
      > >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[/color][/color]
      to[color=blue][color=green]
      > >another form field, changes e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.
      > >
      > >I have searched the net using google, but only found validators that show
      > >an error if an incorrect format is used. (and I can't write one on my[/color][/color]
      own)[color=blue]
      >
      > That can't be done, because the code can't guess whether 1122005 is[/color]
      January 12[color=blue]
      > or November 2. That's why you have to ask the user to correct it.
      >[/color]

      Unless you only reformat the entry with dashes if it has a length of 8.

      <html>
      <head>
      <title>ddmmyyyy .htm</title>
      <script type="text/javascript">
      function format(that) {
      var what = that.value;
      if (what.length != 8) return;
      that.value = what.substr(0,2 ) + "-" + what.substr(2,2 ) + "-" +
      what.substr(4);
      }
      </script>
      </head>
      <body>
      <form>
      <input type="text" name="dmy" size="10" maxlength="10"
      value="01122005 " onblur="format( this)">
      </form>
      </body>
      </html>


      Comment

      • Evertjan.

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

        Lee wrote on 15 mrt 2005 in comp.lang.javas cript:
        [color=blue]
        > Martin Herrman said:[color=green]
        >>
        >>Dear scripters,
        >>
        >>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'.
        >>
        >>I have searched the net using google, but only found validators that
        >>show an error if an incorrect format is used. (and I can't write one
        >>on my own)[/color]
        >
        > That can't be done, because the code can't guess whether 1122005 is
        > January 12 or November 2. That's why you have to ask the user to
        > correct it.[/color]

        'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.

        Given these constraints and
        the boundaries of the "current" century
        it can be done.

        function organize(x){
        if (/^\d{8}$/.test(x))
        x.value = x.replace(/(\d{2})(\d{2})( \d{4})/,'$1-$2-$3')
        else if (/^\d{1,2}\d{1,2} \d{2}$/.test(x)) {
        a = x.split('-')
        x.value = two(a[0])+'-'+two(a[1])+
        '-'+(+a[2]>30)?'19'+a[2]:'20'+a[2]
        }
        }

        function two(x){
        return (x<10)?'0'+x:x
        }

        ...............

        <input onblur='organiz e(this)'>

        --
        Evertjan.
        The Netherlands.
        (Replace all crosses with dots in my emailaddress)

        Comment

        • Evertjan.

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

          Evertjan. wrote on 15 mrt 2005 in comp.lang.javas cript:
          [color=blue]
          > 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.
          >
          > Given these constraints and
          > the boundaries of the "current" century
          > it can be done.
          >
          > function organize(x){
          > if (/^\d{8}$/.test(x))
          > x.value = x.replace(/(\d{2})(\d{2})( \d{4})/,'$1-$2-$3')
          > else if (/^\d{1,2}\d{1,2} \d{2}$/.test(x)) {[/color]

          else if (/^\d{1,2}-\d{1,2}-\d{2}$/.test(x)) {
          [color=blue]
          > a = x.split('-')
          > x.value = two(a[0])+'-'+two(a[1])+
          > '-'+(+a[2]>30)?'19'+a[2]:'20'+a[2]
          > }
          >}
          >
          > function two(x){
          > return (x<10)?'0'+x:x
          >}
          >[/color]



          --
          Evertjan.
          The Netherlands.
          (Replace all crosses with dots in my emailaddress)

          Comment

          • Mr.Clean

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

            In article <Xns961ADF3A934 8Deejj99@194.10 9.133.29>,
            exjxw.hannivoor t@interxnl.net says...[color=blue]
            > Lee wrote on 15 mrt 2005 in comp.lang.javas cript:
            >[color=green]
            > > Martin Herrman said:[color=darkred]
            > >>
            > >>Dear scripters,
            > >>
            > >>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][/color][/color]


            Instead of using a textbox, maybe you could try dynamic listboxes
            instead. That way, when the month listbox is clicked, the appropriate
            amount of days are filled in the days listbox.

            This way, you wouldn't have to worry if they put in a bad character or
            not.

            Comment

            • Lee

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

              Evertjan. said:[color=blue]
              >
              >Lee wrote on 15 mrt 2005 in comp.lang.javas cript:
              >[color=green]
              >> Martin Herrman said:[color=darkred]
              >>>
              >>>Dear scripters,
              >>>
              >>>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'.
              >>>
              >>>I have searched the net using google, but only found validators that
              >>>show an error if an incorrect format is used. (and I can't write one
              >>>on my own)[/color]
              >>
              >> That can't be done, because the code can't guess whether 1122005 is
              >> January 12 or November 2. That's why you have to ask the user to
              >> correct it.[/color]
              >
              >'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.
              >
              >Given these constraints and
              >the boundaries of the "current" century
              >it can be done.[/color]

              But you can't assume those constraints.

              Comment

              • McKirahan

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

                "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
                news:d17foe01ll 4@drn.newsguy.c om...[color=blue]
                > Martin Herrman said:[color=green]
                > >
                > >Dear scripters,
                > >
                > >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[/color][/color]
                to[color=blue][color=green]
                > >another form field, changes e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.
                > >
                > >I have searched the net using google, but only found validators that show
                > >an error if an incorrect format is used. (and I can't write one on my[/color][/color]
                own)[color=blue]
                >
                > That can't be done, because the code can't guess whether 1122005 is[/color]
                January 12[color=blue]
                > or November 2. That's why you have to ask the user to correct it.
                >[/color]

                "... e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'." doesn't include
                'd-mm-yyy';
                therefore, '1122005' is November 2, 2005.

                Unless the examples ("e.g.") were not definitive.



                Comment

                • Evertjan.

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

                  Lee wrote on 15 mrt 2005 in comp.lang.javas cript:
                  [color=blue][color=green]
                  >>'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.
                  >>
                  >>Given these constraints and
                  >>the boundaries of the "current" century
                  >>it can be done.[/color]
                  >
                  > But you can't assume those constraints.
                  >[/color]

                  They are given by the OP.

                  Why improving on a reasonable request,
                  instead of showing the asked code?


                  --
                  Evertjan.
                  The Netherlands.
                  (Replace all crosses with dots in my emailaddress)

                  Comment

                  • Zifud

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

                    McKirahan wrote:
                    [...][color=blue]
                    >
                    > "... e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'." doesn't include
                    > 'd-mm-yyy';
                    > therefore, '1122005' is November 2, 2005.[/color]

                    Based on the suggested format, I make it 1 December 2005, or it
                    may be 11 February 2005.

                    Or does ddmmyyyy really mean mmddyyyy?



                    --
                    Zif

                    Comment

                    • McKirahan

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

                      "Zifud" <Zifud@hotmail. com> wrote in message
                      news:eIJZd.336$ Zn.22757@news.o ptus.net.au...[color=blue]
                      > McKirahan wrote:
                      > [...][color=green]
                      > >
                      > > "... e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'." doesn't include
                      > > 'd-mm-yyy';
                      > > therefore, '1122005' is November 2, 2005.[/color]
                      >
                      > Based on the suggested format, I make it 1 December 2005, or it
                      > may be 11 February 2005.
                      >
                      > Or does ddmmyyyy really mean mmddyyyy?
                      >
                      > --
                      > Zif[/color]

                      I should have said:

                      therefore, '1122005' is 11-Feb-2005.

                      Thanks.


                      Comment

                      • Lee

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

                        Evertjan. said:[color=blue]
                        >
                        >Lee wrote on 15 mrt 2005 in comp.lang.javas cript:
                        >[color=green][color=darkred]
                        >>>'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.
                        >>>
                        >>>Given these constraints and
                        >>>the boundaries of the "current" century
                        >>>it can be done.[/color]
                        >>
                        >> But you can't assume those constraints.
                        >>[/color]
                        >
                        >They are given by the OP.
                        >
                        >Why improving on a reasonable request,
                        >instead of showing the asked code?[/color]

                        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?

                        Comment

                        • Morris

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

                          "McKirahan" <News@McKirahan .com> a écrit dans le message de
                          news:ktSdnSww67 ci8KrfRVn-3g@comcast.com. ..[color=blue]
                          > "Zifud" <Zifud@hotmail. com> wrote in message
                          > news:eIJZd.336$ Zn.22757@news.o ptus.net.au...[color=green]
                          > > McKirahan wrote:
                          > > [...][color=darkred]
                          > > >
                          > > > "... e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'." doesn't include
                          > > > 'd-mm-yyy';
                          > > > therefore, '1122005' is November 2, 2005.[/color]
                          > >
                          > > Based on the suggested format, I make it 1 December 2005, or it
                          > > may be 11 February 2005.
                          > >
                          > > Or does ddmmyyyy really mean mmddyyyy?
                          > >
                          > > --
                          > > Zif[/color]
                          >
                          > I should have said:
                          >
                          > therefore, '1122005' is 11-Feb-2005.
                          >
                          > Thanks.
                          >
                          >[/color]

                          '1122005' is either 1-Dec-2005 or 11-Feb-2005.

                          To remove any ambiguity, you should always use two digits for the day and
                          the month.


                          Comment

                          • McKirahan

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

                            "Morris" <morris2010@lyc os.com> wrote in message
                            news:d3LZd.3475 1$nT2.1587675@w eber.videotron. net...
                            [color=blue]
                            > '1122005' is either 1-Dec-2005 or 11-Feb-2005.
                            >
                            > To remove any ambiguity, you should always use two digits for the day and
                            > the month.[/color]

                            Duh! Jumping in a little late aren't we...

                            You should have read the full thread first.


                            Comment

                            • Matt Kruse

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

                              Lee wrote:[color=blue]
                              > 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.

                              --
                              Matt Kruse



                              Comment

                              Working...