value is a number

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

    value is a number

    I'm trying to test if a value is a number.

    I thought I could do this:

    var test='not_a_num ber';
    var test2='4.00';

    num_test=parseF loat(test);

    if(num_test == 'NaN'){alert('t hat was not a number')}

    But that doesn't work. (at least not in NS)

    Maybe a regex?

    Jeff




  • Douglas Crockford

    #2
    Re: value is a number

    > I'm trying to test if a value is a number.[color=blue]
    >
    > I thought I could do this:
    >
    > var test='not_a_num ber';
    > var test2='4.00';
    >
    > num_test=parseF loat(test);
    >
    > if(num_test == 'NaN'){alert('t hat was not a number')}
    >
    > But that doesn't work. (at least not in NS)
    >
    > Maybe a regex?[/color]

    Use the isNaN() function, or better, use the isFinite() function.

    if (!isFinite(test )) {alert(test + ' is not a number');}

    NaN is a freaky thing. The comparison operators don't work with it.


    Comment

    • Dr John Stockton

      #3
      Re: value is a number

      JRS: In article <QXrub.6092$Rk5 .4193@newsread1 .news.atl.earth link.net>,
      seen in news:comp.lang. javascript, Jeff Thies <nospam@nospam. net> posted
      at Tue, 18 Nov 2003 16:40:16 :-[color=blue]
      > I'm trying to test if a value is a number.
      >
      > I thought I could do this:
      >
      >var test='not_a_num ber';
      >var test2='4.00';
      >
      >num_test=parse Float(test);
      >
      >if(num_test == 'NaN'){alert('t hat was not a number')}
      >
      > But that doesn't work. (at least not in NS)[/color]

      NaN is not a number; since this might occur in many ways, it is equal to
      nothing, not even itself. But 'NaN' is a perfectly good non-numeric
      string, and +'NaN' gives NaN without being equal to it.

      [color=blue]
      >Maybe a regex?[/color]

      Yes. In any reasonable application in which it is right to validate a
      number, it is likely that the set of suitable numbers is much smaller
      than the set of possible numbers. Likewise formats.

      You may in practice want non-negative integers, with no need to allow
      the format 1e2; in that case test with RegExp /^\d+$/ - see
      <URL:http://www.merlyn.demo n.co.uk/js-maths.htm#Valid >.

      Once the format is OK, you can convert the string to a number in safety
      with unary +
      Numeric = +Stringy.value
      and then if necessary do further tests with arithmetic comparison.

      E.G. : In US notation, seconds format is RegExp /^[0-5]\d$/ = 00..59 but
      there is nothing quite so simple for hours 01..12 ; /^0[1-9]|1[0-2]$/ .

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