textboxes addition

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

    #1

    textboxes addition

    I have a very simple webpage (html form). There are about 50 input
    textboxes and users are expected to insert data. No I would like to
    know how in javascript I can check the values in all textboxes not to
    exceed 1000 in total. I want to click the button that submits the data
    on the server, to fire a javascript check the values and if less than
    1000 then send them over otherwrise popup a message window and say
    values exceed 1000.

    Thanks a lot
    Marios Koumides

  • McKirahan

    #2
    Re: textboxes addition

    <koumides@gmail .com> wrote in message
    news:1109858535 .814085.194020@ l41g2000cwc.goo glegroups.com.. .[color=blue]
    > I have a very simple webpage (html form). There are about 50 input
    > textboxes and users are expected to insert data. No I would like to
    > know how in javascript I can check the values in all textboxes not to
    > exceed 1000 in total. I want to click the button that submits the data
    > on the server, to fire a javascript check the values and if less than
    > 1000 then send them over otherwrise popup a message window and say
    > values exceed 1000.
    >
    > Thanks a lot
    > Marios Koumides
    >[/color]

    Will this help? Watch for word-wrap.

    <html>
    <head>
    <title>max1000. htm</title>
    <script type="text/javascript">
    function bytes() {
    var max = 0;
    for (var i=0; i<document.form s[0].elements.lengt h; i++) {
    if (document.forms[0].elements[i].type == "text") {
    max += document.forms[0].elements[i].value.length;
    }
    }
    window.status = max;
    if (max <= 1000) return true;
    alert("1,000 character maximum exceeded!");
    return false;
    }
    </script>
    </head>
    <body>
    <form onsubmit="retur n bytes()">
    <input type="text" name="t001" value="" onblur="bytes() ">
    <input type="text" name="t002" value="" onblur="bytes() ">
    </form>
    </body>
    </html>


    Comment

    • Michael Winter

      #3
      Re: textboxes addition

      McKirahan wrote:

      [snip]
      [color=blue]
      > max += document.forms[0].elements[i].value.length;[/color]

      I think the OP is talking about adding the values, not the number of
      characters (which isn't necessarily the same as the number of bytes,
      by the way).

      If I'm correct, the line above should be altered to

      max += +document.forms[0].elements[i].value;

      though max wouldn't be an appropriate variable name. It would also be
      sensible to check the format of each entry.
      [color=blue]
      > window.status = max;[/color]

      Please don't play around with the status bar, or encourage others to
      do so.

      [snip]
      [color=blue]
      > <form onsubmit="retur n bytes()">[/color]

      Rather than look-up the form reference in the validation function, why
      not pass a reference to the form as an argument. It can be done here
      using the 'this' operator, and in form controls using 'this.form'.
      [color=blue]
      > <input type="text" name="t001" value="" onblur="bytes() ">
      > <input type="text" name="t002" value="" onblur="bytes() ">[/color]

      Validating on the blur event is usually a very bad idea. The main
      reason is that's its a pain in the ass to be told /every time/ a
      control loses focus that something is wrong. Especially if you're
      navigating by keyboard and you're just trying to get to the value you
      want to change. Moreover, if the focus is changed in code, it can lead
      to an endless loop. Whilst that doesn't occur here, its a good reason
      not to demonstrate the approach at all.

      Leave the validation until submission.

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Ivo

        #4
        Re: textboxes addition

        "Michael Winter" spoke of[color=blue]
        > ... the number of characters (which isn't necessarily the same as
        > the number of bytes, by the way).[/color]

        somebody asked in this newsgroup last week what the relation was between
        bytes and characters, and I was very interested, but nobody answered. Do you
        have an idea how to calculate one from the other?
        [color=blue][color=green]
        > > window.status = max;[/color]
        >
        > Please don't play around with the status bar, or encourage others to
        > do so.[/color]

        I must say, I find this use of the statusbar relatively innocent. Not sure
        about other browsers, but in IE it is the only window component that will
        update its display while a javascript is running. I 'm sure they built it
        like that to allow us to 'play with it' and see what our code is doing while
        it is doing it. As long as it doesn't stand in the way of normal statusbar
        behaviour.

        --
        Ivo


        Comment

        • Michael Winter

          #5
          Re: textboxes addition

          Ivo wrote:
          [color=blue]
          > "Michael Winter" spoke of
          >[color=green]
          >> ... the number of characters (which isn't necessarily the same as
          >> the number of bytes, by the way).[/color]
          >
          > somebody asked in this newsgroup last week what the relation was
          > between bytes and characters, and I was very interested, but nobody
          > answered. Do you have an idea how to calculate one from the other?[/color]

          Not particularly, I'm just well aware of the difference. It would
          depend on the encoding you're going to use. Encoding a Unicode string
          with UTF-8 could result in sequences of one, two, three, or four bytes
          for each character. UTF-16 can require one or two 16-bit code units
          (so two or four bytes). Only once you decide on an encoding can you
          determine the relationship.

          [snip]
          [color=blue]
          > I must say, I find this use of the statusbar relatively innocent.[/color]

          I wasn't trying to imply otherwise. However, I just think the status
          bar isn't something to be messed with. If using alert boxes is too
          intrusive, I dump output into a TEXTAREA (which is probably superior
          to an alert in some respects).

          Another reason for objecting in this context is that readers might not
          understand that it's use is only for debugging purposes. That was the
          point of the "encouragem ent" remark; readers might take it as an
          indication that the status bar is a toy.

          [snip]

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          Working...