Text and Date Manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sgershon@gmail.com

    #1

    Text and Date Manipulation

    Hi.

    I know this is should be a simple question.
    I know server-side web-programming, and never needed to use client-side
    scripting... until now :)

    I have done so far a little number of scripts that work well. But there
    are two that I am having special difficulties with:

    1)
    I have a SELECT type input, "document.form_ name.select_inp ut".
    I have a TEXTAREA type input, "document.form_ name.text_area" .
    I have a BUTTON "button".
    When "button" is pressed, "text_area" s value should be
    "text_area"+"se lect_input"s value.

    2)
    I have a TEXT type input "document.form_ name.date1".
    I have a TEXT type input "document.form_ name.date2".
    I have a TEXT type input "document.form_ name.days".
    When any of the first two changes (to a date in the format
    "dd/mm/yyyy"), the third one should contain the difference between the
    dates, in days.

    Any help with these?

    Thanks;
    SGershon

  • McKirahan

    #2
    Re: Text and Date Manipulation

    <sgershon@gmail .com> wrote in message
    news:1110833269 .026789.180050@ l41g2000cwc.goo glegroups.com.. .[color=blue]
    > Hi.
    >
    > I know this is should be a simple question.
    > I know server-side web-programming, and never needed to use client-side
    > scripting... until now :)
    >
    > I have done so far a little number of scripts that work well. But there
    > are two that I am having special difficulties with:
    >
    > 1)
    > I have a SELECT type input, "document.form_ name.select_inp ut".
    > I have a TEXTAREA type input, "document.form_ name.text_area" .
    > I have a BUTTON "button".
    > When "button" is pressed, "text_area" s value should be
    > "text_area"+"se lect_input"s value.
    >
    > 2)
    > I have a TEXT type input "document.form_ name.date1".
    > I have a TEXT type input "document.form_ name.date2".
    > I have a TEXT type input "document.form_ name.days".
    > When any of the first two changes (to a date in the format
    > "dd/mm/yyyy"), the third one should contain the difference between the
    > dates, in days.
    >
    > Any help with these?
    >
    > Thanks;
    > SGershon
    >[/color]

    It sounds like a school assignment.


    Comment

    • SGershon

      #3
      Re: Text and Date Manipulation

      :)
      No, it is not a school homework. I can see that the question is written
      as one, maybe cause I used to teach in a kinder-garden... :)

      I am 26 years old, so if it was a school assignment, my teacher would
      be real mad at me! lol!

      I have done simple pages on HTML/PHP, and went very well without
      JavaScript (I know its powerfull and has lots of capabilities). Now one
      of the scripts I need to do has to change many things dynamically
      before submitting it. Many of the functions I've already done and they
      are working, but these two keep giving me errors or wrong results.

      Thanks for the education concern, anyway!
      And thanks for any help you can give me.

      Comment

      • kaeli

        #4
        Re: Text and Date Manipulation

        In article <1110833269.026 789.180050@l41g 2000cwc.googleg roups.com>,
        sgershon@gmail. com enlightened us with...[color=blue]
        >
        > I have done so far a little number of scripts that work well. But there
        > are two that I am having special difficulties with:[/color]

        Neither are very hard.
        Are you sure this isn't homework?
        [color=blue]
        >
        > 1)
        > I have a SELECT type input, "document.form_ name.select_inp ut".
        > I have a TEXTAREA type input, "document.form_ name.text_area" .
        > I have a BUTTON "button".
        > When "button" is pressed, "text_area" s value should be
        > "text_area"+"se lect_input"s value.
        >[/color]

        Hints:
        -- onclick
        -- document.form_n ame.select_inpu t.options
        [document.form_n ame.select_inpu t.selectedIndex].value
        -- document.form_n ame.text_area.v alue
        -- NaN / isNaN
        [color=blue]
        > 2)
        > I have a TEXT type input "document.form_ name.date1".
        > I have a TEXT type input "document.form_ name.date2".
        > I have a TEXT type input "document.form_ name.days".
        > When any of the first two changes (to a date in the format
        > "dd/mm/yyyy"), the third one should contain the difference between the
        > dates, in days.[/color]

        Hints:
        -- onChange
        -- http://www.w3schools.com/js/js_obj_date.asp

        HTH

        --
        --
        ~kaeli~
        If you don't pay your exorcist, you get repossessed.



        Comment

        • kaeli

          #5
          Re: Text and Date Manipulation

          In article <MPG.1c9fc0a966 44d83698a36c@nn tp.lucent.com>,
          tiny_one@NOSPAM .comcast.net enlightened us with...[color=blue]
          > In article <1110833269.026 789.180050@l41g 2000cwc.googleg roups.com>,
          > sgershon@gmail. com enlightened us with...[color=green]
          > >
          > > I have done so far a little number of scripts that work well. But there
          > > are two that I am having special difficulties with:[/color]
          >
          > Neither are very hard.
          > Are you sure this isn't homework?
          >[/color]

          Oh, if it IS homework and you want it completed, just sent me $30 (I take
          Paypal).
          I'll be happy to do your work for you. :p

          --
          --
          ~kaeli~
          If a turtle doesn't have a shell, is he homeless or naked?



          Comment

          • Mick White

            #6
            Re: Text and Date Manipulation

            sgershon@gmail. com wrote:[color=blue]
            > Hi.
            >
            > I know this is should be a simple question.
            > I know server-side web-programming, and never needed to use client-side
            > scripting... until now :)
            >
            > I have done so far a little number of scripts that work well. But there
            > are two that I am having special difficulties with:
            >
            > 1)
            > I have a SELECT type input, "document.form_ name.select_inp ut".
            > I have a TEXTAREA type input, "document.form_ name.text_area" .
            > I have a BUTTON "button".
            > When "button" is pressed, "text_area" s value should be
            > "text_area"+"se lect_input"s value.
            >
            > 2)
            > I have a TEXT type input "document.form_ name.date1".
            > I have a TEXT type input "document.form_ name.date2".
            > I have a TEXT type input "document.form_ name.days".
            > When any of the first two changes (to a date in the format
            > "dd/mm/yyyy"), the third one should contain the difference between the
            > dates, in days.[/color]

            Such an endeavour is fraught with danger; relying on a user to enter the
            correct format, that is.

            days.value= Math.floor ((new Date (date1.value)-new Date
            (date2.value))/(1000*60*60*24) );

            Need some error checking, though.
            Mick




            Comment

            • McKirahan

              #7
              Re: Text and Date Manipulation

              "SGershon" <sgershon@gmail .com> wrote in message
              news:1110836426 .165438.88440@l 41g2000cwc.goog legroups.com...[color=blue]
              > :)
              > No, it is not a school homework. I can see that the question is written
              > as one, maybe cause I used to teach in a kinder-garden... :)
              >
              > I am 26 years old, so if it was a school assignment, my teacher would
              > be real mad at me! lol!
              >
              > I have done simple pages on HTML/PHP, and went very well without
              > JavaScript (I know its powerfull and has lots of capabilities). Now one
              > of the scripts I need to do has to change many things dynamically
              > before submitting it. Many of the functions I've already done and they
              > are working, but these two keep giving me errors or wrong results.
              >
              > Thanks for the education concern, anyway!
              > And thanks for any help you can give me.
              >[/color]

              Will this get you started? Watch for word-wrap.

              This uses Mick White's formula for "days".

              <html>
              <head>
              <title>SGershon .htm</title>
              <script type="text/javascript">
              function append(form) {
              var what =
              form.select_inp ut.options[form.select_inp ut.selectedInde x].value;
              form.text_area. value += what;
              }
              function calc(form) {
              var dat1 = form.date1.valu e;
              if (dat1.length != 10) return;
              var dat2 = form.date2.valu e;
              if (dat2.length != 10) return;
              form.days.value = Math.floor((new Date(dat2)-new
              Date(dat1))/(1000*60*60*24) );
              }
              </script>
              </head>
              <body>
              <form>
              <select name="select_in put">
              <option value="1">1
              <option value="2">2
              </select>
              <textarea name="text_area " cols="30" rows="10"></textarea>
              <input type="button" onclick="append (this.form)">
              <br><br>
              <input type="text" name="date1" size="10" maxlength="10"
              onchange="calc( this.form)">
              <input type="text" name="date2" size="10" maxlength="10"
              onchange="calc( this.form)">
              <input type="text" name="days" size="5" maxlength="6" readonly> days
              difference
              </form>
              </body>
              </html>



              Comment

              • RobG

                #8
                Re: Text and Date Manipulation

                sgershon@gmail. com wrote:[color=blue]
                > Hi.
                >
                > I know this is should be a simple question.[/color]

                Questions are easy, it's the answers that cause headaches :-)

                [...][color=blue]
                > I have done so far a little number of scripts that work well. But there
                > are two that I am having special difficulties with:[/color]
                [...]

                The practical part of your question (getting the content of form
                elements and doing something with it) has been answered above.
                The remaining issue is how to reliably capture dates entered by
                a user.

                You have two options:

                1. Allow them to enter text and validate it thoroughly

                2. Present a 'fool-proof' date picker that will only allow valid
                dates to be entered.

                The choice between the two depends on your users. If they are
                entering lots of dates or reasonably computer and keyboard
                literate, they will prefer to use option 1. If they are casual
                users who only need to enter single dates infrequently, option
                2 may suit better (or offer both).

                Finally, never trust user-entered dates and always check the
                content at the server.

                Since no one has pointed you at Dr J's site for date stuff, here
                it is:

                <URL:http://www.merlyn.demo n.co.uk/js-dates.htm>

                Take the time to read and understand the issues here, I think
                you'll find whatever you want.

                A last word on date formats: the primary ISO standard is
                yyyy-mm-dd, however other formats are suggested also.

                Please read an excellent discussion of ISO8601 date/time formats
                (recommended by the w3c) by Markus Kuhn here:

                <URL:http://www.cl.cam.ac.u k/~mgk25/iso-time.html>


                --
                Rob

                Comment

                • SGershon

                  #9
                  Re: Text and Date Manipulation

                  Thanks McKirahan, for your help.
                  Thanks to everbody else that cared to reply as well.

                  The code you suggested worked perfectly by itself, but then I had a
                  little problems when using it in my page.
                  After a while, I managed to get the problems sorted.
                  The append is appending, and the days calculator is calculating.

                  I have a question on each function:
                  1) In the end of the append function, I do an text_area.focus (). It is
                  focusing, but the cursor stays at the first character.
                  How can I move the cursos to the end in order to make the user's work
                  easier (faster)?

                  2) I am having much trouble trying to use the dd/mm/yyyy format in the
                  date function.
                  Where can I set the "new date()" to use this format? I learned a
                  devguru.com that when building a date object from a string as we are
                  doing, it reads it in the format date.parse() tells. But I cannot see a
                  way to change from mm/dd/yyyy to dd/mm/yyyy!
                  Can you help me on this?

                  Thanks!

                  Comment

                  • SGershon

                    #10
                    Re: Text and Date Manipulation

                    Hi.

                    Thanks to everybody for the concerns.

                    I am using a date-picker to enter the date at the textboxes. Textboxes
                    are disabled to prevent direct editting.
                    [color=blue]
                    > Finally, never trust user-entered dates and always check the
                    > content at the server.[/color]

                    I am checking the content by javascript at the client.
                    But now I am thinking (because you wrote "at the server"), that if the
                    user does not have javascript enabled I do not know what I am receiving
                    at the server... Is that why you said to check it at the server?
                    Also, if the user has JS disabled... he wont get the date-picker. Now
                    that I realised this my solution is to have the "disabled" text written
                    by a javacript document.write, cause I think this way user without JS
                    will have the textbox enabled. Is that right?

                    Comment

                    • SGershon

                      #11
                      Re: Text and Date Manipulation

                      Hi.

                      Thanks you all for the concerns.

                      I am using a date picker to insert the date in the textbox, and the
                      textbox is disable to prevent direct editting.

                      You said "always check content at the server".
                      I am checking content at the client using javascript.
                      But now that you said "at the server", I am thinking that if the user
                      has javascript disable, I will have no control over what I am
                      receiving. Is that why you said "at the server"?

                      Now that I think of it, the date-picker will not work if the user has
                      javascript disabled, and he can not edit the textbox content...
                      I am thinking... if I disable the textbox using javascript, users with
                      no javascript will have the textbox enabled. But is there a
                      "document.form. text_box.disabl ed" property?

                      SGershon

                      Comment

                      • RobG

                        #12
                        Re: Text and Date Manipulation

                        SGershon wrote:[color=blue]
                        > Hi.
                        >
                        > Thanks you all for the concerns.
                        >
                        > I am using a date picker to insert the date in the textbox, and the
                        > textbox is disable to prevent direct editting.
                        >
                        > You said "always check content at the server".
                        > I am checking content at the client using javascript.
                        > But now that you said "at the server", I am thinking that if the user
                        > has javascript disable, I will have no control over what I am
                        > receiving. Is that why you said "at the server"?
                        >[/color]

                        There are two reasons to check at the server:

                        1. The user may have JS disabled (or their user agent may not
                        support JS at all)

                        2. The user can 'spoof' the data sent back to your server, that
                        is, the return may not come from your form at all but from
                        some contrived values the user has made up.

                        What you do about the above depends on your requirements. If
                        this is for a commercial web site, you must allow for both cases
                        above - non-JS users are about 10% of the surfer population
                        (this is a debatable statistic, but seems reasonable to me).

                        There is also likely a smaller but more troublesome element that
                        will try to break into your system using various techniques
                        based on spoofing the returned values.

                        OTOH, if this is for an intranet-only application where you can
                        specify JS to be enabled and have more control over users, the
                        first issue is still important but likely not quite as critical
                        (depending on your users, of course!). But the second is still
                        vital: you do not know who can get onto your network or
                        guarantee their behaviour.
                        [color=blue]
                        > Now that I think of it, the date-picker will not work if the user has
                        > javascript disabled, and he can not edit the textbox content...
                        > I am thinking... if I disable the textbox using javascript, users with
                        > no javascript will have the textbox enabled. But is there a
                        > "document.form. text_box.disabl ed" property?[/color]

                        Your date entry field should be enabled by default and disabled
                        by JS and replaced by the 'picker' functionality. Ideally, your
                        site should be just as usable without JS - JS should be used to
                        enhance functionality.

                        You can also use a date selector that doesn't rely on JS -
                        selects with options for year, month and day are common. You
                        can hide the selects with JS and use the picker, or if JS isn't
                        enabled, the selects are displayed.

                        Just don't treat your users like they are dumb - give them
                        information on how to use UI features before they have to find
                        out how they work by accident or trial and error. Stick to
                        conventions and standards wherever appropriate.

                        :-)

                        --
                        Rob

                        Comment

                        • RobG

                          #13
                          Re: Text and Date Manipulation

                          SGershon wrote:
                          [...][color=blue]
                          > 2) I am having much trouble trying to use the dd/mm/yyyy format in the
                          > date function.[/color]

                          Here is a revised version of McKirahan's code that does some
                          date validation. It ensures things like month is between 1 & 12
                          inclusive and that the day is OK for the month (no November 31
                          or February 29 in non-leap years). For a reasonably complete
                          treatment of this topic, have a browse here:

                          <URL:http://www.merlyn.demo n.co.uk/js-date4.htm#VID>

                          My routines are designed to be easily read, Dr J's stuff at the
                          above site is likely more efficient and thorough, but requires a
                          little more work to understand.

                          His site also has algorithms for date arithmetic:

                          <URL:http://www.merlyn.demo n.co.uk/js-date1.htm>

                          Between Dr J's site and the script below, you should be able to
                          work out how to use any date format.


                          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                          "http://www.w3.org/TR/html4/strict.dtd">
                          <html><head>
                          <title> Date Stuff </title>
                          <meta http-equiv="Content-Type"
                          content="text/html; charset=ISO-8859-1">
                          <script type="text/javascript">

                          function calc(f) {
                          var d1 = validateDate(f. date1.value);
                          var d2 = validateDate(f. date2.value);

                          if ( d1 && d2 ){
                          var dat1 = new Date(d1[2],d1[1],d1[0]);
                          var dat2 = new Date(d2[2],d2[1],d2[0]);
                          } else {
                          // alert('Please enter valid dates');
                          f.days.value = 'Error';
                          return false;
                          }
                          f.days.value = Math.floor((dat 2 - dat1)/(1000*60*60*24) );
                          }
                          function validateDate(x) {
                          var minYear = 2000;
                          var maxYear = 2010;

                          // Get the bits of the date
                          var d = x.split('-');
                          // Test year within range
                          if (d[2] < minYear || d[2]> maxYear) {
                          return false;
                          }
                          // Test month is between 1 and 12 inclusive
                          if (d[1] < 1 || d[1] > 12) {
                          return false;
                          // Test month is a 1 or 2 digit integer
                          } else if (! /\d{1,2}/.test(d[1])) {
                          return false;
                          }
                          // Test day is OK for month & year
                          if ( d[0] < 0 || d[0] > getMonthDays(d[2],d[1])) {
                          return false;
                          }
                          return d;
                          }

                          function getMonthDays(Y, M) {
                          return M==4 || M==6 || M==9 || M==11 ? 30 :
                          M==2 ? 28 + (Y%4==0 && ( Y%100!=0 || Y%400==0)) : 31 ;
                          }

                          </script>
                          </head>
                          <body>
                          <form>
                          <p>Please enter dates (dd-mm-yyyy):</p>
                          <input type="text" name="date1" size="10" maxlength="10"
                          onblur="calc(th is.form)">&nbsp ;Date&nbsp;1<br >
                          <input type="text" name="date2" size="10" maxlength="10"
                          onblur="calc(th is.form)">&nbsp ;Date&nbsp;2<br >
                          <input type="text" name="days" size="10" maxlength="6"
                          readonly>&nbsp; Difference&nbsp ;(days)<br>
                          <input type="reset">
                          </form>
                          </body>
                          </html>




                          --
                          Rob

                          Comment

                          • RobG

                            #14
                            Re: Text and Date Manipulation

                            RobG wrote:

                            Aggghhh... perpetuated error, should have checked more
                            thoroughly:

                            [...][color=blue]
                            > if ( d1 && d2 ){
                            > var dat1 = new Date(d1[2],d1[1],d1[0]);
                            > var dat2 = new Date(d2[2],d2[1],d2[0]);[/color]

                            var dat1 = new Date(d1[2],d1[1]-1,d1[0]);
                            var dat2 = new Date(d2[2],d2[1]-1,d2[0]);

                            Must allow for JS dates that are 0 to 11. Sorry :-(




                            --
                            Rob

                            Comment

                            • McKirahan

                              #15
                              Re: Text and Date Manipulation

                              "SGershon" <sgershon@gmail .com> wrote in message
                              news:1110865459 .457406.292280@ l41g2000cwc.goo glegroups.com.. .[color=blue]
                              > Thanks McKirahan, for your help.
                              > Thanks to everbody else that cared to reply as well.
                              >
                              > The code you suggested worked perfectly by itself, but then I had a
                              > little problems when using it in my page.
                              > After a while, I managed to get the problems sorted.
                              > The append is appending, and the days calculator is calculating.
                              > I have a question on each function:
                              > 1) In the end of the append function, I do an text_area.focus (). It is
                              > focusing, but the cursor stays at the first character.
                              > How can I move the cursos to the end in order to make the user's work
                              > easier (faster)?[/color]

                              function append(form) {
                              var what =
                              form.select_inp ut.options[form.select_inp ut.selectedInde x].value;
                              form.text_area. value += what;
                              var range = form.text_area. createTextRange ();
                              range.collapse( false);
                              range.select();
                              }

                              Credit to Mark Honnen; perhaps an IE-only solution
                              [color=blue]
                              > 2) I am having much trouble trying to use the dd/mm/yyyy format in the
                              > date function.
                              > Where can I set the "new date()" to use this format? I learned a
                              > devguru.com that when building a date object from a string as we are
                              > doing, it reads it in the format date.parse() tells. But I cannot see a
                              > way to change from mm/dd/yyyy to dd/mm/yyyy!
                              > Can you help me on this?[/color]

                              It's widely recommended that you use the unambiguous ccyy/mm/dd format.
                              [color=blue]
                              > Thanks!
                              >[/color]

                              You're welcome.


                              Comment

                              Working...