Integer Check

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis M. Marks

    Integer Check

    What is the best way to validate a field as a positive or negative
    integer?

    --
    Dennis M. Marks

    Replace domain.invalid with dcsi.net


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
  • William Morris

    #2
    Re: Integer Check

    myvar < 0
    myvar > 0 ?


    "Dennis M. Marks" <denmarks@domai n.invalid> wrote in message
    news:2004200407 33235484%denmar ks@domain.inval id...[color=blue]
    > What is the best way to validate a field as a positive or negative
    > integer?
    >
    > --
    > Dennis M. Marks
    > http://www.dcs-chico.com/~denmarks/
    > Replace domain.invalid with dcsi.net
    >
    >
    > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    > -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]


    Comment

    • Vincent van Beveren

      #3
      Re: Integer Check

      if (isNan(myVariab le)) {
      alert('incorrec t, not a number');
      } else {
      if ((myVariable % 1)!=0) {
      alert('Number can not be a floating-point');
      } else return true;

      return false;
      }


      Comment

      • Dennis M. Marks

        #4
        Re: Integer Check

        In article <40853795$0$558 $e4fe514c@news. xs4all.nl>, Vincent van
        Beveren <vincent@provid ent.remove.this .nl> wrote:
        [color=blue]
        > if (isNan(myVariab le)) {
        > alert('incorrec t, not a number');
        > } else {
        > if ((myVariable % 1)!=0) {
        > alert('Number can not be a floating-point');
        > } else return true;
        >
        > return false;
        > }
        >
        >[/color]

        Thank you (except it has to be isNaN)
        I would never have thought of your way to check for integer.

        --
        Dennis M. Marks

        Replace domain.invalid with dcsi.net


        -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
        http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
        -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

        Comment

        • Mick White

          #5
          Re: Integer Check

          Vincent van Beveren wrote:
          [color=blue]
          > if (isNan(myVariab le)) {
          > alert('incorrec t, not a number');
          > } else {
          > if ((myVariable % 1)!=0) {
          > alert('Number can not be a floating-point');
          > } else return true;
          >
          > return false;
          > }
          >[/color]


          Why not:
          function isInteger(num){
          return (!isNaN(num) && num%1==0);
          }
          ?
          Mick

          Comment

          • Evertjan.

            #6
            Re: Integer Check

            Mick White wrote on 20 apr 2004 in comp.lang.javas cript:
            [color=blue]
            > Vincent van Beveren wrote:
            >[color=green]
            >> if (isNan(myVariab le)) {
            >> alert('incorrec t, not a number');
            >> } else {
            >> if ((myVariable % 1)!=0) {
            >> alert('Number can not be a floating-point');
            >> } else return true;
            >>
            >> return false;
            >> }
            >>[/color]
            >
            >
            > Why not:
            > function isInteger(num){
            > return (!isNaN(num) && num%1==0);
            >}
            > ?
            > Mick
            >[/color]

            return !(isNaN(num) || !(num % 1));

            or

            return !isNaN(num) && !!(num % 1);


            --
            Evertjan.
            The Netherlands.
            (Please change the x'es to dots in my emailaddress)

            Comment

            • Dr John Stockton

              #7
              Re: Integer Check

              JRS: In article <20042004073323 5484%denmarks@d omain.invalid>, seen in
              news:comp.lang. javascript, Dennis M. Marks <denmarks@domai n.invalid>
              posted at Tue, 20 Apr 2004 07:33:23 :
              [color=blue]
              >What is the best way to validate a field as a positive or negative
              >integer?[/color]

              How about a zero one? Do you want to allow a floating-point form of a
              value which happens to be integer, such as 1e6 or 3.000 ? How about
              0xFF ?

              OK = /^[+-]?\d+$/.test(field)
              tests for optional sign followed by only decimal digits. \d{1,6} might
              be preferred, likewise [+- ] .

              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://jibbering.com/faq/> Jim Ley's FAQ for 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

              • Vincent van Beveren

                #8
                Re: Integer Check

                >>Why not:[color=blue][color=green]
                >>function isInteger(num){
                >>return (!isNaN(num) && num%1==0);
                >>}
                >>?
                >>Mick
                >>[/color]
                >
                >
                > return !(isNaN(num) || !(num % 1));
                >
                > or
                >
                > return !isNaN(num) && !!(num % 1);
                >[/color]

                I just wrote the code as a draft. Any of these are good.

                return (!isNaN(num)) && ((num % 1)==0)

                would probably be the most relayable... since num % 1 is a
                calculation, it would be better to see if it matches a number,
                even though 0 == false.

                Comment

                • Evertjan.

                  #9
                  Re: Integer Check

                  Vincent van Beveren wrote on 21 apr 2004 in comp.lang.javas cript:[color=blue][color=green]
                  >> return !(isNaN(num) || !(num % 1));
                  >>
                  >> or
                  >>
                  >> return !isNaN(num) && !!(num % 1);
                  >>[/color]
                  >
                  > I just wrote the code as a draft. Any of these are good.
                  >
                  > return (!isNaN(num)) && ((num % 1)==0)
                  >
                  > would probably be the most relayable... since num % 1 is a
                  > calculation, it would be better to see if it matches a number,
                  > even though 0 == false.[/color]

                  I do not know about this relaying,
                  but any nonzero number is considered true in javascriot.

                  var nonzeronumber = 2;
                  alert(!!nonzero number) // gives true

                  var zeronumber = 0;
                  alert(!!zeronum ber) // gives false

                  --
                  Evertjan.
                  The Netherlands.
                  (Please change the x'es to dots in my emailaddress)

                  Comment

                  • Richard Cornford

                    #10
                    Re: Integer Check

                    Evertjan. wrote:[color=blue]
                    > Vincent van Beveren wrote on 21 apr 2004 in comp.lang.javas cript:[color=green][color=darkred]
                    >>> return !(isNaN(num) || !(num % 1));
                    >>>
                    >>> or
                    >>>
                    >>> return !isNaN(num) && !!(num % 1);
                    >>>[/color]
                    >>
                    >> I just wrote the code as a draft. Any of these are good.
                    >>
                    >> return (!isNaN(num)) && ((num % 1)==0)
                    >>
                    >> would probably be the most relayable... since num % 1 is a
                    >> calculation, it would be better to see if it matches a number,
                    >> even though 0 == false.[/color]
                    >
                    > I do not know about this relaying,
                    > but any nonzero number is considered true in javascriot.
                    >
                    > var nonzeronumber = 2;
                    > alert(!!nonzero number) // gives true
                    >
                    > var zeronumber = 0;
                    > alert(!!zeronum ber) // gives false[/color]

                    If there is a desire to explicitly return a boolean value, but avoid the
                    extra comparisons, it might be practical to apply the NOT operator to
                    the result of the whole expression:-

                    return !(isNaN(num)||( num % 1));

                    Richard.


                    Comment

                    Working...