integer checking ?

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

    integer checking ?

    hi,

    is there any function in Javascript to check/validate integer value.
    there is parseInt(val) but it only checks first digit.

    example : to find error in 123k44

  • Randy Webb

    #2
    Re: integer checking ?

    mhk wrote:[color=blue]
    > hi,
    >
    > is there any function in Javascript to check/validate integer value.
    > there is parseInt(val) but it only checks first digit.
    >
    > example : to find error in 123k44
    >[/color]

    myVar = "123k44"

    if (myVar == +myVar)

    lightly tested.

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    • mhk

      #3
      Re: integer checking ?



      Randy Webb wrote:
      [color=blue]
      > mhk wrote:
      >[color=green]
      >> hi,
      >>
      >> is there any function in Javascript to check/validate integer value.
      >> there is parseInt(val) but it only checks first digit.
      >>
      >> example : to find error in 123k44
      >>[/color]
      >
      > myVar = "123k44"
      >
      > if (myVar == +myVar)
      >
      > lightly tested.
      >[/color]
      *************** ***************
      yes ... looks like its working but what that "+" does ?
      Thanks

      Comment

      • RobG

        #4
        Re: integer checking ?

        Randy Webb wrote:
        [...][color=blue][color=green]
        >> is there any function in Javascript to check/validate integer value.
        >> there is parseInt(val) but it only checks first digit.
        >>
        >> example : to find error in 123k44
        >>[/color]
        >
        > myVar = "123k44"
        >
        > if (myVar == +myVar)[/color]

        But that will "validate" any number, not just integers.

        If the requirement is that the variable only contains digits (that is,
        no decimal point or any other character), if:

        var isInt = /^\d+$/.test(a)

        then isInt will be true only if 'a' contains only digits.

        However, 0.2e3 is a valid int (it is equivalent to 200) - is that OK
        with the OP? And 5.0 is a valid int too, but will fail the test.

        Have a read here:

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

        and here:

        <URL:http://www.jibbering.c om/faq/faq_notes/type_convert.ht ml#tcUserIn>

        --
        Rob

        Comment

        • Evertjan.

          #5
          Re: integer checking ?

          mhk wrote on 16 jan 2005 in comp.lang.javas cript:
          [color=blue]
          > hi,
          >
          > is there any function in Javascript to check/validate integer value.
          > there is parseInt(val) but it only checks first digit.[/color]
          [color=blue]
          > example : to find error in 123k44[/color]


          <script>
          if (isInteger('123 k44'))
          alert('yes: 123k44')
          else
          alert('no: 123k44')

          if (isInteger('123 e44'))
          alert('yes: 123e44')
          else
          alert('no: 123e44')

          if (isInteger('123 .44'))
          alert('yes: 123.44')
          else
          alert('no: 123.44')


          function isInteger(n) {
          return (!isNaN(n)) && (Math.floor(n)= =n)
          }

          </script>

          --
          Evertjan.
          The Netherlands.
          (Replace all crosses with dots in my emailaddress)

          Comment

          • Richard Cornford

            #6
            Re: integer checking ?

            Evertjan. wrote:
            <snip>[color=blue]
            > function isInteger(n) {
            > return (!isNaN(n)) && (Math.floor(n)= =n)
            > }[/color]
            <snip>

            As NaN is specified as never equalling NaN and Math.floor(NaN) results
            in NaN:-

            function isInteger(n){
            return (Math.floor(n) == n);
            }

            - would be simpler.

            Richard.


            Comment

            • Dr John Stockton

              #7
              Re: integer checking ?

              JRS: In article <cse4ah$fpl$1$8 30fa7a5@news.de mon.co.uk>, dated Sun, 16
              Jan 2005 16:21:35, seen in news:comp.lang. javascript, Richard Cornford
              <Richard@litote s.demon.co.uk> posted :[color=blue]
              >Evertjan. wrote:
              ><snip>[color=green]
              >> function isInteger(n) {
              >> return (!isNaN(n)) && (Math.floor(n)= =n)
              >> }[/color]
              ><snip>
              >
              >As NaN is specified as never equalling NaN and Math.floor(NaN) results
              >in NaN:-
              >
              >function isInteger(n){
              > return (Math.floor(n) == n);
              >}
              >
              >- would be simpler.[/color]

              isInteger("1000 000000000000000 000000000000000 000000000000.3" )
              isInteger("9007 199254740993")
              isInteger("3e+3 33")
              isInteger("3e-333")
              isInteger("0xbe ad")

              give true.


              Before testing for integer, one should first consider what one really
              should be testing for; usually, one wants a digit in [1-9] followed by
              not too many decimal digits. The OP should use a RegExp.

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
              <URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
              <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

              Comment

              • sancha

                #8
                Re: integer checking ?

                function isAllDigitsint( argvalue) {
                argvalue = argvalue.toStri ng();
                var validChars = "0123456789 ";
                var startFrom = 0;
                if (argvalue.subst ring(0, 2) == "0x") {
                validChars = "0123456789abcd efABCDEF";
                startFrom = 2;
                } else if (argvalue.charA t(0) == "0") {
                validChars = "01234567";
                startFrom = 1;
                } else if (argvalue.charA t(0) == "-") {
                startFrom = 1;
                }

                for (var n = startFrom; n < argvalue.length ; n++) {
                if (validChars.ind exOf(argvalue.s ubstring(n, n+1))
                == -1) return false;
                }
                return true;



                checks to see if all digits are int
                parseInt this and u should get ur result
                Problem ???
                this accepts octal.
                and 09 fails .. need to improve the script.
                Please help

                Comment

                Working...