Is numeric?

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

    Is numeric?

    I may have cross-posted this... :-(

    I'm a VB programmer getting *way* to deep into this wonderful new JS
    venture. I looked around for a function like VBs' IsNumeric without much
    success. I had to roll my own. Does anyone see any bugs in this?

    function IsNumeric(expre ssion) {

    var nums = "0123456789 ";

    if (expression.len gth==0)return(f alse);

    for (var n=0; n < expression.leng th; n++){

    if(nums.indexOf (expression.cha rAt(n))==-1)return(false) ;


    }

    return(true);

    }


  • Mark

    #2
    Re: Is numeric?

    One glaringly easier approach would be just to do something like this:
    either
    if (isNaN(expressi on))return false;
    or
    if (isNaN(parseInt (expression)))r eturn false;
    or (assuming floating-point numbers are possible in your expression)
    if (isNaN(parseFlo at(expression)) )return false;

    One of those should fit what you're looking for. For more info on isNaN(),
    just do a google search. Hell, if you use isNaN(), you don't need your
    custom function in the first place. It's built in (just opposite of VB).

    Good luck on your javascript adventures,
    Matt

    "Aaron DeLoach" <aaron@deloachc orp.com> wrote in message
    news:jvqdnWezaa gNe7iiU-KYvA@eatel.net. ..[color=blue]
    > I may have cross-posted this... :-(
    >
    > I'm a VB programmer getting *way* to deep into this wonderful new JS
    > venture. I looked around for a function like VBs' IsNumeric without much
    > success. I had to roll my own. Does anyone see any bugs in this?
    >
    > function IsNumeric(expre ssion) {
    >
    > var nums = "0123456789 ";
    >
    > if (expression.len gth==0)return(f alse);
    >
    > for (var n=0; n < expression.leng th; n++){
    >
    > if(nums.indexOf (expression.cha rAt(n))==-1)return(false) ;
    >
    >
    > }
    >
    > return(true);
    >
    > }
    >
    >[/color]


    Comment

    • Aaron DeLoach

      #3
      Re: Is numeric?

      I tried the isNaN() with unsatisfactory results. I don't quite understand
      *how* it works but your post chopped the code from eight lines to three (I
      like efficient code). I'll have to read the M$ link you provided. Thanks
      for your help.

      "Steve van Dongen" <stevevd@hotmai l.com> wrote in message
      news:441civkqcb i7mqh58gjbcmktf a3cpeanst@4ax.c om...[color=blue]
      > On Mon, 28 Jul 2003 23:13:42 -0600, "Mark" <bigmw@charter. net> wrote:
      >[color=green]
      > >One glaringly easier approach would be just to do something like this:
      > >either
      > >if (isNaN(expressi on))return false;
      > >or
      > >if (isNaN(parseInt (expression)))r eturn false;
      > >or (assuming floating-point numbers are possible in your expression)
      > >if (isNaN(parseFlo at(expression)) )return false;
      > >
      > >One of those should fit what you're looking for. For more info on[/color][/color]
      isNaN(),[color=blue][color=green]
      > >just do a google search. Hell, if you use isNaN(), you don't need your
      > >custom function in the first place. It's built in (just opposite of VB).
      > >
      > >Good luck on your javascript adventures,
      > >Matt
      > >
      > >"Aaron DeLoach" <aaron@deloachc orp.com> wrote in message
      > >news:jvqdnWeza agNe7iiU-KYvA@eatel.net. ..[color=darkred]
      > >> I may have cross-posted this... :-(
      > >>
      > >> I'm a VB programmer getting *way* to deep into this wonderful new JS
      > >> venture. I looked around for a function like VBs' IsNumeric without[/color][/color][/color]
      much[color=blue][color=green][color=darkred]
      > >> success. I had to roll my own. Does anyone see any bugs in this?
      > >>
      > >> function IsNumeric(expre ssion) {
      > >>
      > >> var nums = "0123456789 ";
      > >>
      > >> if (expression.len gth==0)return(f alse);
      > >>
      > >> for (var n=0; n < expression.leng th; n++){
      > >>
      > >> if(nums.indexOf (expression.cha rAt(n))==-1)return(false) ;
      > >>
      > >>
      > >> }
      > >>
      > >> return(true);
      > >>
      > >> }[/color][/color]
      >
      > I just wanted to note that[color=green]
      > >if (isNaN(parseInt (expression)))r eturn false;[/color]
      > isn't a very good test because it allows bad things through. For
      > example, parseInt("40lak jsdlfj") is 40, which is a number, but the
      > original input string (obviously) isn't. It also blocks valid inputs
      > like "09" which is a valid decimal number but not a valid octal
      > number.
      >
      > Aaron, that function of yours should work fine. You could also use a
      > regular expression.
      >[/color]
      http://msdn.microsoft.com/library/en...gexpsyntax.asp[color=blue]
      >
      > function IsNumeric(expre ssion)
      > {
      > return (String(express ion).search(/^\d+$/) != -1);
      > }
      >
      > Regards,
      > Steve[/color]


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Is numeric?

        Steve van Dongen <stevevd@hotmai l.com> writes:
        [color=blue]
        > I just wanted to note that[color=green]
        > >if (isNaN(parseInt (expression)))r eturn false;[/color]
        > isn't a very good test because it allows bad things through. For
        > example, parseInt("40lak jsdlfj") is 40, which is a number, but the
        > original input string (obviously) isn't. It also blocks valid inputs
        > like "09" which is a valid decimal number but not a valid octal
        > number.[/color]

        Valid point. Try this instead

        function isNumeric(value ) {
        return typeof value != "boolean" && value !== null && !isNaN(+ value);
        }

        The prefix "+" converts its argument to a number just like the Number
        function.
        The extra checks are there because booleans and null can be converted
        to the numbers 0 and 1.
        [color=blue]
        > return (String(express ion).search(/^\d+$/) != -1);[/color]

        You can make this a little shorter:
        return String(expressi on).match(/^\d+$/);

        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Dr John Stockton

          #5
          Re: Is numeric?

          JRS: In article <jvqdnWezaagNe7 iiU-KYvA@eatel.net> , seen in
          news:comp.lang. javascript, Aaron DeLoach <aaron@deloachc orp.com> posted
          at Mon, 28 Jul 2003 22:14:23 :-[color=blue]
          >I may have cross-posted this... :-([/color]

          You did not; perhaps you mean multi-posted.
          [color=blue]
          > I looked around for a function like VBs' IsNumeric without much
          >success.[/color]

          Without an accurate knowledge of the VB function it is difficult to
          emulate it reliably. For example. what about an empty string?

          function IsNumeric(S) { return S > '' && ! isNaN(S) }

          seems reasonable. But, for me, it accepts 1e9999 - Infinity is a
          number.

          function IsNumeric(S) { return S > '' && isFinite(S) }

          However, in any practical application, the permissible input is likely
          to be more limited; use a RegExp to test for, say, 1..5 decimal digits
          preceded by sign or space.

          OK = /^[-+ ]?\d{1,5}$/.test(S)

          --
          © 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> JS maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

          Comment

          Working...