blonde question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Olson

    #1

    blonde question...

    I need to do a if statement on only textfields that have a number in them
    other then zero. What is the best way to filter out all field values other
    then numbers other then zero? I haven't had any luck, I know this should be
    a simple thing, but I'm having a blonde day.


  • John Bokma

    #2
    Re: blonde question...

    Terry Olson wrote:
    [color=blue]
    > I need to do a if statement on only textfields that have a number in
    > them other then zero. What is the best way to filter out all field
    > values other then numbers other then zero? I haven't had any luck, I
    > know this should be a simple thing, but I'm having a blonde day.[/color]

    Paint your hair :-D

    Get all textfields, check if they have a number, and check if it's zero or
    not.

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html

    Comment

    • Terry Olson

      #3
      Re: blonde question...

      Thank you for turning my question into a statement, and completely avoiding
      answering it. Anyone out there want to give me SPECIFICS?

      "John Bokma" <postmaster@cas tleamber.com> wrote in message
      news:Xns957F9C3 C97DEEcastleamb er@130.133.1.4. ..[color=blue]
      > Terry Olson wrote:
      >[color=green]
      > > I need to do a if statement on only textfields that have a number in
      > > them other then zero. What is the best way to filter out all field
      > > values other then numbers other then zero? I haven't had any luck, I
      > > know this should be a simple thing, but I'm having a blonde day.[/color]
      >
      > Paint your hair :-D
      >
      > Get all textfields, check if they have a number, and check if it's zero or
      > not.
      >
      > --
      > John MexIT: http://johnbokma.com/mexit/
      > personal page: http://johnbokma.com/
      > Experienced programmer available: http://castleamber.com/
      > Happy Customers: http://castleamber.com/testimonials.html[/color]


      Comment

      • John Bokma

        #4
        Re: blonde question...

        Terry Olson wrote:

        Don't top post
        [color=blue]
        > "John Bokma" <postmaster@cas tleamber.com> wrote in message[/color]
        [color=blue][color=green]
        >> Get all textfields, check if they have a number, and check if it's
        >> zero or not.[/color][/color]
        [color=blue]
        > Thank you for turning my question into a statement, and completely
        > avoiding answering it. Anyone out there want to give me SPECIFICS?[/color]

        You asked how, my answer is one way. But you probably want a complete
        spelled out solution?

        --
        John MexIT: http://johnbokma.com/mexit/
        personal page: http://johnbokma.com/
        Experienced programmer available: http://castleamber.com/
        Happy Customers: http://castleamber.com/testimonials.html

        Comment

        • Michael Winter

          #5
          Re: blonde question...

          On Mon, 11 Oct 2004 19:42:30 GMT, Terry Olson <twjolson@hotma il.com> wrote:
          [color=blue]
          > I need to do a if statement on only textfields that have a number in
          > them other then zero. What is the best way to filter out all field
          > values other then numbers other then zero? I haven't had any luck, I
          > know this should be a simple thing, but I'm having a blonde day.[/color]

          It would be simple if you were more specific.

          When are you checking the text fields? Are you checking *every* text
          input, or just a subset?

          When these fields contain numbers, should they only contain numbers, and
          what do you mean by "filter out"?

          About the only thing I can say with certainty is that your answer lies
          with regular expressions.

          Mike

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

          Comment

          • mscir

            #6
            Re: blonde question...


            Michael Winter wrote:[color=blue]
            > On Mon, 11 Oct 2004 19:42:30 GMT, Terry Olson <twjolson@hotma il.com> wrote:
            >[color=green]
            >> I need to do a if statement on only textfields that have a number in
            >> them other then zero. What is the best way to filter out all field
            >> values other then numbers other then zero? I haven't had any luck, I
            >> know this should be a simple thing, but I'm having a blonde day.[/color]
            >
            > It would be simple if you were more specific.
            >
            > When are you checking the text fields? Are you checking *every* text
            > input, or just a subset?
            >
            > When these fields contain numbers, should they only contain numbers,
            > and what do you mean by "filter out"?
            >
            > About the only thing I can say with certainty is that your answer lies
            > with regular expressions.
            >
            > Mike[/color]

            What about something like this to check every textbox and return every
            non-zero value?

            <script type="text/javascript">
            function checktextboxes( ){
            var txtvalue;
            var elements = document.getEle mentsByTagName( 'input');
            for (var i=0; i<elements.leng th; i++) {
            if (elements.item( i).type=="text" ) {
            txtvalue = elements.item(i ).value;
            if ((parseFloat(tx tvalue)) && (parseFloat(txt value)!=0))
            alert(txtvalue) ;
            }
            }
            }
            </script>

            <input type="text" value="10"><br>
            <input type="text" value="5"><br>
            <input type="text" value="1"><br>
            <input type="text" value="0"><br>
            <input type="text" value="-1"><br>
            <input type="text" value="-5"><br>
            <input type="text" value="-10"><br>
            <input type="text" value="asdf"><b r>
            <input type="button" value="check textbox values"
            onclick="checkt extboxes()">

            Mike

            Comment

            • MikeB

              #7
              Re: blonde question...


              "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
              news:opsfq4noa5 x13kvk@atlantis ...[color=blue]
              > On Mon, 11 Oct 2004 19:42:30 GMT, Terry Olson <twjolson@hotma il.com> wrote:
              >[color=green]
              > > I need to do a if statement on only textfields that have a number in
              > > them other then zero. What is the best way to filter out all field
              > > values other then numbers other then zero? I haven't had any luck, I
              > > know this should be a simple thing, but I'm having a blonde day.[/color]
              >
              > It would be simple if you were more specific.
              >
              > When are you checking the text fields? Are you checking *every* text
              > input, or just a subset?[/color]

              If a group of textfields within a larger group is to be treated differently than
              the others, I prefix the name of the field with a couple of meaningful identical
              characters, like if the field is "Required", a field name might be
              "reqFirstNa me" and the like, then looping through the fields I can address just
              those fields to a test, rather than every field. This logic could be employed
              here.
              [color=blue]
              > When these fields contain numbers, should they only contain numbers, and
              > what do you mean by "filter out"?
              >
              > About the only thing I can say with certainty is that your answer lies
              > with regular expressions.
              >
              > Mike
              >
              > --
              > Michael Winter
              > Replace ".invalid" with ".uk" to reply by e-mail.[/color]


              Comment

              • Fred Oz

                #8
                Re: blonde question...

                mscir wrote:
                [snip][color=blue]
                >
                > What about something like this to check every textbox and return every
                > non-zero value?
                >[/color]
                [snip]

                Whilst your function neatly cycles through all the text boxes, it
                doesn't test very rigorously.

                Much more validation is required - your function will return every
                value that parseFloat() can turn into a number. e.g. "5ABC" will
                return "5" and be accepted by your function as a non-zero number.

                The quickest (and dirtiest perhaps...) test I know of is:

                if (x == parseFloat(x)) alert(x + ' is a float');

                It will accept any number format, including scientific notation. Of
                course, it doesn't test the format or suitability of the number for
                whatever purpose you want it for (maybe you want an integer).

                If you want more rigour or format validation, regular expression tests
                are the go.


                Cheers, Fred.

                Comment

                • Lee

                  #9
                  Re: blonde question...

                  Terry Olson said:[color=blue]
                  >
                  >I need to do a if statement on only textfields that have a number in them
                  >other then zero. What is the best way to filter out all field values other
                  >then numbers other then zero? I haven't had any luck, I know this should be
                  >a simple thing, but I'm having a blonde day.[/color]

                  Coding is easier once you've learned to state the problem clearly
                  and concisely. If you're thinking of the problem as to "filter out
                  all fields values other than numbers other than zero" you're bound
                  to be confused.

                  If you restate it as to "<take some action> on all text fields that
                  contain numbers that are not zero", it's much simpler:

                  <html>
                  <head>
                  <title>demo</title>
                  <script type="text/javascript">
                  function demo(f){
                  for (var i=0;i<f.element s.length;i++) {
                  if(f.elements[i].type=="text" &&
                  !isNaN(f.elemen ts[i].value) &&
                  0 != +f.elements[i].value) {
                  // do something with f.elements[i]
                  alert(f.element s[i].value);
                  }
                  }
                  }
                  </script>
                  </head>
                  <body>
                  <form>
                  <input value="">
                  <input value="3">
                  <input value="0">
                  <input value="apple">
                  <br>
                  <input type="button" value="test" onclick="demo(t his.form)">
                  </form>
                  </body>
                  </html>

                  Comment

                  • Lee

                    #10
                    Re: blonde question...

                    mscir said:
                    [color=blue]
                    > var elements = document.getEle mentsByTagName( 'input');[/color]

                    I would avoid using getElementsByTa gName(), since there is a much
                    older mechanism that works just as well, assuming that you're not
                    interested in accumulating elements from multiple forms.

                    var elements = thisForm.elemen ts;

                    Your method will filter out the non-input elements, but you still
                    have to check the "type" attribute, and the older method will work
                    in more clients.

                    Comment

                    • Dr John Stockton

                      #11
                      Re: blonde question...

                      JRS: In article <GmBad.74338$a4 1.62715@pd7tw2n o>, dated Mon, 11 Oct
                      2004 19:42:30, seen in news:comp.lang. javascript, Terry Olson
                      <twjolson@hotma il.com> posted :[color=blue]
                      >I need to do a if statement on only textfields that have a number in them
                      >other then zero. What is the best way to filter out all field values other
                      >then numbers other then zero? I haven't had any luck, I know this should be
                      >a simple thing, but I'm having a blonde day.[/color]

                      Firstly, you have to define zero.

                      Is 0.0e6 zero, or is it not?

                      Do you actually mean number, or do you mean digit? Many posters do not
                      discriminate.

                      Is there a number in "Daddy flew a B-36", and if so is it negative?

                      x = /[1-9]/.test(S) // Does S contain a digit other than zero?

                      Does "Pi11" contain a number?

                      Must the number be the only non-white-space? Does leading/trailing
                      white-space matter?

                      If you can say that the only characters to be allowed in the fields are
                      decimal digits, the question becomes much easier.

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

                      • jmm-list-gn

                        #12
                        Re: blonde question...

                        Terry Olson wrote:[color=blue]
                        > I need to do a if statement on only textfields that have a number in them
                        > other then zero. What is the best way to filter out all field values other
                        > then numbers other then zero? I haven't had any luck, I know this should be
                        > a simple thing, but I'm having a blonde day.
                        >[/color]
                        - Get the text.
                        - Convert it to a number.
                        - Test it for (non-)zero.

                        --
                        jmm dash list (at) sohnen-moe (dot) com
                        (Remove .AXSPAMGN for email)

                        Comment

                        • mscir

                          #13
                          Re: blonde question...

                          Lee wrote:[color=blue]
                          > mscir said:
                          >[color=green]
                          >> var elements = document.getEle mentsByTagName( 'input');[/color]
                          >
                          > I would avoid using getElementsByTa gName(), since there is a much
                          > older mechanism that works just as well, assuming that you're not
                          > interested in accumulating elements from multiple forms.
                          >
                          > var elements = thisForm.elemen ts;
                          >
                          > Your method will filter out the non-input elements, but you still
                          > have to check the "type" attribute, and the older method will work
                          > in more clients.[/color]

                          Good point Lee, thanks,
                          Mike

                          Comment

                          Working...