Form Validation Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank Bishop

    Form Validation Question

    I don't know very much yet and I'm looking to do some form validation on
    20 form fields that accept numbers. I would like to alert the user if
    the combined values of any of the fields is greater than 40 (It's a 40
    point survey).

    Should I loop thru all the form fields when they tab out of each one to
    check this (onblur)? I'm thinking it might be nice to let them know how
    far over they are in the alert as well.

    Code snip appreciated.

    Thanks,
    Frank

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Jacky

    #2
    Re: Form Validation Question

    You may display a text of the total point somewhere and the onblur
    event will add to that amount and check if it exceeds 40. Then you do
    not need to loop through fields. Just remember to implement the adding
    and subtracting of that total amount correctly.

    Comment

    • Jacky

      #3
      Re: Form Validation Question

      You may display a text of the total point somewhere and the onblur
      event will add to that amount and check if it exceeds 40. Then you do
      not need to loop through fields. Just remember to implement the adding
      and subtracting of that total amount correctly.

      Comment

      • Hal Rosser

        #4
        Re: Form Validation Question


        "Jacky" <jackysee@gmail .com> wrote in message
        news:1103346573 .742441.210800@ c13g2000cwb.goo glegroups.com.. .[color=blue]
        > You may display a text of the total point somewhere and the onblur
        > event will add to that amount and check if it exceeds 40. Then you do
        > not need to loop through fields. Just remember to implement the adding
        > and subtracting of that total amount correctly.
        >[/color]

        and watch out for the shift-tab.
        The user could tab backwards after filling out part of the form
        I would recomment looping thru all each time.
        Also watch out for NaN entries. If you're coding for numbers, you can be
        sure the user will be typing the Gettysberg address into your text box -
        or - paste a chapter of War and Peace from another website.



        ---
        Outgoing mail is certified Virus Free.
        Checked by AVG anti-virus system (http://www.grisoft.com).
        Version: 6.0.818 / Virus Database: 556 - Release Date: 12/17/2004


        Comment

        • RobB

          #5
          Re: Form Validation Question


          Frank Bishop wrote:[color=blue]
          > I don't know very much yet and I'm looking to do some form validation[/color]
          on[color=blue]
          > 20 form fields that accept numbers. I would like to alert the user if
          > the combined values of any of the fields is greater than 40 (It's a[/color]
          40[color=blue]
          > point survey).
          >
          > Should I loop thru all the form fields when they tab out of each one[/color]
          to[color=blue]
          > check this (onblur)? I'm thinking it might be nice to let them know[/color]
          how[color=blue]
          > far over they are in the alert as well.
          >
          > Code snip appreciated.
          >
          > Thanks,
          > Frank
          >
          > *** Sent via Developersdex http://www.developersdex.com ***
          > Don't just participate in USENET...get rewarded for it![/color]

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
          <html>
          <head>
          <title>untitled </title>
          <style type="text/css">

          body {
          font-size: 100%;
          }
          input.n {
          width: 50px;
          font: normal 70% tahoma;
          text-align: center;
          }
          div#readout {
          width: 240px;
          height: 16px;
          font: normal 70% tahoma;
          text-align: center;
          padding: 2px;
          margin-left: 40px;
          border: 1px #000 solid;
          background: buttonface;
          }
          div#warning {
          visibility: hidden;
          width: 240px;
          height: 16px;
          font: normal 70% tahoma;
          color: #f00;
          text-align: center;
          padding: 2px;
          margin-top: 10px;
          margin-left: 40px;
          border: 1px #000 solid;
          background: #ff0;
          }

          </style>
          <script type="text/javascript">
          //<![CDATA[

          function set_fields()
          {

          function summit()
          {
          var i = 0,
          el,
          n,
          b,
          sum = 0;
          while (el = els.item(i++))
          if (isNaN(n = Number(el.value )))
          {
          el.value = 'invalid';
          }
          else sum += n;
          var t = document.create TextNode('Combi ned value: ');
          var s = document.create TextNode(sum);
          el = document.getEle mentById('reado ut');
          while (el.hasChildNod es())
          el.removeChild( el.lastChild);
          el.appendChild( t);
          n = document.create Element('strong ');
          n.appendChild(s );
          el.appendChild( n);
          el = document.getEle mentById('warni ng');
          if (sum > 40)
          {
          t = 'You have exceeded the allowed value by ' + (sum - 40) + ' !';
          n = document.create TextNode(t);
          while (el.hasChildNod es())
          el.removeChild( el.lastChild);
          el.appendChild( n);
          el.style.visibi lity = 'visible';
          }
          else el.style.visibi lity = 'hidden';
          }

          var i = 0,
          el,
          els = document.getEle mentsByTagName( 'input');
          while (el = els.item(i++))
          if (/\bn\b/.test(el.classN ame))
          el.onchange = summit;
          }

          onload = set_fields;

          //]]>
          </script>
          </head>
          <body>
          <form>
          <ol>
          <li><input id="n1" class="n" type="text" name="n1" value="" /></li>
          <li><input id="n2" class="n" type="text" name="n2" value="" /></li>
          <li><input id="n3" class="n" type="text" name="n3" value="" /></li>
          <li><input id="n4" class="n" type="text" name="n4" value="" /></li>
          <li><input id="n5" class="n" type="text" name="n5" value="" /></li>
          <li><input id="n6" class="n" type="text" name="n6" value="" /></li>
          <li><input id="n7" class="n" type="text" name="n7" value="" /></li>
          <li><input id="n8" class="n" type="text" name="n8" value="" /></li>
          <li><input id="n9" class="n" type="text" name="n9" value="" /></li>
          <li><input id="n10" class="n" type="text" name="n10" value="" /></li>
          <li><input id="n11" class="n" type="text" name="n11" value="" /></li>
          <li><input id="n12" class="n" type="text" name="n12" value="" /></li>
          <li><input id="n13" class="n" type="text" name="n13" value="" /></li>
          <li><input id="n14" class="n" type="text" name="n14" value="" /></li>
          <li><input id="n15" class="n" type="text" name="n15" value="" /></li>
          <li><input id="n16" class="n" type="text" name="n16" value="" /></li>
          <li><input id="n17" class="n" type="text" name="n17" value="" /></li>
          <li><input id="n18" class="n" type="text" name="n18" value="" /></li>
          <li><input id="n19" class="n" type="text" name="n19" value="" /></li>
          <li><input id="n20" class="n" type="text" name="n20" value="" /></li>
          </ol>
          </form>
          <div id="readout"></div>
          <div id="warning"></div>
          </body>
          </html>

          Just a rough approximation.. .

          Comment

          • RobB

            #6
            Re: Form Validation Question

            Oops....meant to do this:

            function summit()
            {
            var i = 1,
            el,
            n,
            b,
            sum = 0;
            while (el = document.getEle mentById('n' + i++))
            .........and so on

            Comment

            • Michael Winter

              #7
              Re: Form Validation Question

              On 17 Dec 2004 21:09:33 -0800, Jacky <jackysee@gmail .com> wrote:

              Please quote relevant text from the previous article when replying to a
              post.
              [color=blue]
              > You may display a text of the total point somewhere and the onblur event
              > will add to that amount and check if it exceeds 40.[/color]

              The blur event isn't usually suitable for validation, particularly if
              you're going to add alerts. Use the change event instead.

              [snip]

              Mike

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

              Comment

              • Frank Bishop

                #8
                Re: Form Validation Question

                Thanks Rob for this great code I can build from. Thanks to all for
                suggestions. I'll be able to use this example for a lot of my form
                creations.

                Sincerely,
                Frank


                *** Sent via Developersdex http://www.developersdex.com ***
                Don't just participate in USENET...get rewarded for it!

                Comment

                • Dr John Stockton

                  #9
                  Re: Form Validation Question

                  JRS: In article <41c3b5ef$1_2@1 27.0.0.1>, dated Fri, 17 Dec 2004
                  22:45:35, seen in news:comp.lang. javascript, Frank Bishop
                  <fbishop@viper. com> posted :
                  [color=blue]
                  >I don't know very much yet and I'm looking to do some form validation on
                  >20 form fields that accept numbers. I would like to alert the user if
                  >the combined values of any of the fields is greater than 40 (It's a 40
                  >point survey).[/color]

                  For each field, let the onChange event check that it contains no non-
                  digit; if it fails, alert and set focus back; if it passes, add up all
                  the fields (interpret "" as "0") and show them in a readonly type-text
                  control. If that total is too big, you could disable the Submit button.
                  A field known to contain no non-digit can be converted to a number with
                  a unary + sign : N = +field.value .

                  OK = !/\D/.test(field.val ue)

                  You may wish, or not need, to trim leading & trailing spaces; see FAQ.

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

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