Loading ...
Sorry, an error occurred while loading the content.

parseInt Function for text field validation?

Expand Messages
  • flashqa1
    hello everyone: I am doing an exercise for validating form element(s) passed to external JavaScript. My approach is that I used a for loop to go through the
    Message 1 of 8 , Apr 28, 2007
    • 0 Attachment
      hello everyone:

      I am doing an exercise for validating form element(s) passed to
      external JavaScript.

      My approach is that I used a for loop to go through the text string,
      filter out the character data and keep the digits only. It worked for
      cases like n45, 3#3 etc, but when user entered 09 as an entry, I only
      got '0' as an output. I could not figure out why the number 9 got left
      off. Could someone look my code and give me some suggestions?

      My text fild's maxlength is 3, so user can enter only 3 characters at
      a time.

      JavaScript code:
      function age_check()
      {
      var ageStr = document.account_form.age.value;

      var temp;
      var age="";

      //use for loop to go through the string get from name text field
      for(i = 0; i < ageStr.length; i++)
      {
      temp = ageStr.charAt(i);
      if(!isNaN(temp)) //filter out character data in string keep the digits
      {
      age +=temp;
      }

      }

      //parse string into integer;
      var intAge = parseInt(age);

      alert(intAge);
      alert(typeof(intAge));

      }

      Jeff
      4-28-09
    • Emmanuel
      ... Not exactly the reply you expected but you may want to know about, and use, regular expressions. For instance the following will remove any non-digit
      Message 2 of 8 , Apr 29, 2007
      • 0 Attachment
        At 7:20 PM +0000 4/28/07, flashqa1 wrote:
        >hello everyone:
        >
        >I am doing an exercise for validating form element(s) passed to
        >external JavaScript.
        >
        >My approach is that I used a for loop to go through the text string,
        >filter out the character data and keep the digits only.

        Not exactly the reply you expected but you may want to know about,
        and use, regular expressions. For instance the following will remove
        any non-digit character from the variable s:

        s = s.replace ( /[^0-9]/g , "" )

        Emmanuel
        <http://www.quomodo.com>
      • Jon Stephens
        ... Interesting. Both Firefox and Konqueror/kjscmd exhibit this behaviour for 08 and 09 , whereas Opera returns 8 and 9. In fact, FF and Konq both return 0
        Message 3 of 8 , Apr 29, 2007
        • 0 Attachment
          > parseInt Function for text field validation?
          > Posted by: "flashqa1" flashqa1@... flashqa1
          > Date: Sun Apr 29, 2007 2:26 am ((PDT))
          >
          > hello everyone:
          >
          > I am doing an exercise for validating form element(s) passed to
          > external JavaScript.
          >
          > My approach is that I used a for loop to go through the text string,
          > filter out the character data and keep the digits only. It worked for
          > cases like n45, 3#3 etc, but when user entered 09 as an entry, I only
          > got '0' as an output. I could not figure out why the number 9 got left
          > off. Could someone look my code and give me some suggestions?
          >
          > My text fild's maxlength is 3, so user can enter only 3 characters at
          > a time.
          >
          > JavaScript code:
          > function age_check()
          > {
          > var ageStr = document.account_form.age.value;
          >
          > var temp;
          > var age="";
          >
          > //use for loop to go through the string get from name text field
          > for(i = 0; i < ageStr.length; i++)
          > {
          > temp = ageStr.charAt(i);
          > if(!isNaN(temp)) //filter out character data in string keep the digits
          > {
          > age +=temp;
          > }
          >
          > }
          >
          > //parse string into integer;
          > var intAge = parseInt(age);
          >
          > alert(intAge);
          > alert(typeof(intAge));
          >
          > }
          >
          > Jeff
          > 4-28-09

          Interesting. Both Firefox and Konqueror/kjscmd exhibit this behaviour
          for "08" and "09", whereas Opera returns 8 and 9. In fact, FF and Konq
          both return 0 for parseInt(s) where s is *any* string beginning with
          "08" or "09".

          I've no idea why this is so, I'm afraid.

          However, I can tell you that I seldom if ever use parseInt() or
          parseFloat() - instead, I subtract zero to force the conversion to a
          number (or to NaN as the case may be). I guess that I can I can now
          claim a method to my (alleged) madness in doing so. ;)

          In all three browsers, "08" - 0 yields 8, "09" - 0 yields 9, "091" - 0
          yields 91, etc., as one would expect.

          Alternatively, you can use the Number constructor; Number("09") also
          yields 9 and Number("091") yields 91.

          cheers

          jon.

          --
          This message has not been scanned for viruses.

          Since I do not use a Microsoft operating
          system or software, and use only plaintext
          for email, there is little need for me to do so.
        • Jonas Raoni
          ... In the case you don t know, the parseInt has a different behaviour for strings starting with 0x (hexadecimal) or 0 (octal) if you don t specify the base
          Message 4 of 8 , Apr 29, 2007
          • 0 Attachment
            On 4/29/07, Jon Stephens <jon@...> wrote:
            > Interesting. Both Firefox and Konqueror/kjscmd exhibit this behaviour
            > for "08" and "09", whereas Opera returns 8 and 9. In fact, FF and Konq
            > both return 0 for parseInt(s) where s is *any* string beginning with
            > "08" or "09".
            >
            > I've no idea why this is so, I'm afraid.

            In the case you don't know, the parseInt has a different behaviour for
            strings starting with 0x (hexadecimal) or 0 (octal) if you don't
            specify the "base" in the second parameter. Which doesn't happen with
            its brother parseFloat -- parseFloat("09") --.

            So the "08" and "09" should be considerated part of the octal system,
            which don't have the digit 8 nor 9, so in my opinion the correct value
            should be NaN instead of 0 since the number is invalid (if --
            parseInt("0x-") -- was 0 too, it would be ok to return 0 in the
            previous case).

            > However, I can tell you that I seldom if ever use parseInt() or
            > parseFloat() - instead, I subtract zero to force the conversion to a
            > number (or to NaN as the case may be). I guess that I can I can now
            > claim a method to my (alleged) madness in doing so. ;)

            For the statement -- alert(1 + "23") --, I personally prefer --
            alert(1 + +"23") -- instead of
            -- alert(1 + ("23" - 0)) -- which is "noisy" in my opinion.

            Anyway the "parseInt" is useful if you need to convert through bases
            -- parseInt("11", 2) -- or remove the float part of a number --
            parseInt("123.321") --, and both parseInt and parseFloat will work
            with -- alert(parseInt("1z")) --, whereas alert(+"1z") won't give the
            same results.


            --
            Jonas Raoni Soares Silva
            http://www.jsfromhell.com
          • Bocah Sableng
            ... var intAge = +age; Works on IE6 and FF1.5 IMHO, this happens because some browsers treat number starting with zero as octal. So characters like 8 and 9
            Message 5 of 8 , Apr 29, 2007
            • 0 Attachment
              On 4/29/07, flashqa1 <flashqa1@...> wrote:

              > hello everyone:
              >
              > I am doing an exercise for validating form element(s) passed to
              > external JavaScript.
              >
              > My approach is that I used a for loop to go through the text string,
              > filter out the character data and keep the digits only. It worked for
              > cases like n45, 3#3 etc, but when user entered 09 as an entry, I only
              > got '0' as an output. I could not figure out why the number 9 got left
              > off. Could someone look my code and give me some suggestions?
              >
              > My text fild's maxlength is 3, so user can enter only 3 characters at
              > a time.
              >
              > JavaScript code:
              > function age_check()
              > {
              > var ageStr = document.account_form.age.value;
              >
              > var temp;
              > var age="";
              >
              > //use for loop to go through the string get from name text field
              > for(i = 0; i < ageStr.length; i++)
              > {
              > temp = ageStr.charAt(i);
              > if(!isNaN(temp)) //filter out character data in string keep the digits
              > {
              > age +=temp;
              > }
              >
              > }
              >
              > //parse string into integer;
              > var intAge = parseInt(age);

              var intAge = +age;

              Works on IE6 and FF1.5

              IMHO, this happens because some browsers treat number starting with
              zero as octal. So characters like '8' and '9' is invalid.

              HTH
            • Jonas Raoni
              ... This is smaller -- s.replace (/ D/g, ) -- =) For such counts involving text fields I often do something like -- alert(+input.value.replace(/[^ d.-]/g,
              Message 6 of 8 , Apr 30, 2007
              • 0 Attachment
                On 4/29/07, Emmanuel <satimage@...> wrote:
                > s = s.replace ( /[^0-9]/g , "" )

                This is smaller -- s.replace (/\D/g, "") -- =)

                For such counts involving text fields I often do something like --
                alert(+input.value.replace(/[^\d.-]/g, "") || 0) --, then I won't
                receive "NaN" as result. But in my opinion it sounds better to warn
                the user when its input is invalid.


                --
                Jonas Raoni Soares Silva
                http://www.jsfromhell.com
              • David Smart
                Another comment that might be unexpected ... Don t strip non-numerics from fields, just get the user to fix them. Otherwise, a typo can leave you with a
                Message 7 of 8 , Apr 30, 2007
                • 0 Attachment
                  Another comment that might be unexpected ...

                  Don't strip non-numerics from fields, just get the user to fix them.

                  Otherwise, a typo can leave you with a totally wrong number.

                  Regards, Dave S

                  ----- Original Message -----
                  From: "Emmanuel" <satimage@...>
                  To: <JavaScript_Official@yahoogroups.com>
                  Sent: Sunday, April 29, 2007 8:10 PM
                  Subject: Re: [JavaScript] parseInt Function for text field validation?


                  > At 7:20 PM +0000 4/28/07, flashqa1 wrote:
                  >>hello everyone:
                  >>
                  >>I am doing an exercise for validating form element(s) passed to
                  >>external JavaScript.
                  >>
                  >>My approach is that I used a for loop to go through the text string,
                  >>filter out the character data and keep the digits only.
                  >
                  > Not exactly the reply you expected but you may want to know about,
                  > and use, regular expressions. For instance the following will remove
                  > any non-digit character from the variable s:
                  >
                  > s = s.replace ( /[^0-9]/g , "" )
                  >
                  > Emmanuel
                  > <http://www.quomodo.com>
                  >
                  >
                  > Visit http://aiaiai.com for more groups to join
                  > Yahoo! Groups Links
                  >
                  >
                  >
                  >
                • tresschwartzs
                  One technique that is invaluable for testing Javascript is using the javascript: prefix in Firefox. For example, to test your parseInt, put the following in
                  Message 8 of 8 , Apr 30, 2007
                  • 0 Attachment
                    One technique that is invaluable for testing Javascript is using the
                    javascript: prefix in Firefox. For example, to test your parseInt, put
                    the following in your Firefox address window and hit enter:

                    javascript: alert(parseInt("09"))

                    You should get an alert with the value 0.

                    Also, don't forget the Firebug debugger. Install that and then take a
                    look at the code.


                    doug

                    --- In JavaScript_Official@yahoogroups.com, "flashqa1" <flashqa1@...>
                    wrote:
                    >
                    > hello everyone:
                    >
                    > I am doing an exercise for validating form element(s) passed to
                    > external JavaScript.
                    >
                    > My approach is that I used a for loop to go through the text string,
                    > filter out the character data and keep the digits only. It worked for
                    > cases like n45, 3#3 etc, but when user entered 09 as an entry, I only
                    > got '0' as an output. I could not figure out why the number 9 got left
                    > off. Could someone look my code and give me some suggestions?
                    >
                    > My text fild's maxlength is 3, so user can enter only 3 characters at
                    > a time.
                    >
                    > JavaScript code:
                    > function age_check()
                    > {
                    > var ageStr = document.account_form.age.value;
                    >
                    > var temp;
                    > var age="";
                    >
                    > //use for loop to go through the string get from name text field
                    > for(i = 0; i < ageStr.length; i++)
                    > {
                    > temp = ageStr.charAt(i);
                    > if(!isNaN(temp)) //filter out character data in string keep the
                    digits
                    > {
                    > age +=temp;
                    > }
                    >
                    > }
                    >
                    > //parse string into integer;
                    > var intAge = parseInt(age);
                    >
                    > alert(intAge);
                    > alert(typeof(intAge));
                    >
                    > }
                    >
                    > Jeff
                    > 4-28-09
                    >
                  Your message has been successfully submitted and would be delivered to recipients shortly.