Validate number with JS?

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

    Validate number with JS?

    Hello there,

    It's possible to check if it's a valid number in <INPUT...

    I try typeof and parseFloat and it's not working. It's seem impossible to
    test (with a if) the value "NaN"!!

    Regards





  • Laurent Bugnion, GalaSoft

    #2
    Re: Validate number with JS?

    Hi,

    Jeff wrote:[color=blue]
    > Hello there,
    >
    > It's possible to check if it's a valid number in <INPUT...
    >
    > I try typeof and parseFloat and it's not working. It's seem impossible to
    > test (with a if) the value "NaN"!!
    >
    > Regards[/color]

    var strValue = document.formNa me.textFieldNam e.value;
    var fValue = parseFloat( strValue );

    if ( isNaN( fValue ) )
    {
    document.formNa me.textFieldNam e.focus();
    document.formNa me.textFieldNam e.select();
    alert( "Please enter a valid number!" );
    }

    HTH,

    Laurent
    --
    Laurent Bugnion, GalaSoft
    Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Validate number with JS?

      "Jeff" <profcegep@hotm ail.com> writes:
      [color=blue]
      > It's possible to check if it's a valid number in <INPUT...[/color]

      Good.
      [color=blue]
      > I try typeof and parseFloat and it's not working. It's seem impossible to
      > test (with a if) the value "NaN"!![/color]

      What is a valid number? You need to answer that before coding anything.

      If a valid number is just a sequence of digits, i.e., only a positive
      integer, then the answer is different from if a valid number can be
      a negative fractional number in engineering notation (e.g., -2.4E-10).

      Just checking against NaN ought to be simple, but it isn't. For some
      reason (NaN === NaN) is false. NaN is not equal to any number value,
      not even itself. Spooky. Luckily, there is a native function for just
      that comparison:

      if (!isNaN(parseFl oat(formRef.ele ments['field'].value))) { ... }

      /L
      --
      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

      • Lee

        #4
        Re: Validate number with JS?

        Jeff said:[color=blue]
        >
        >Hello there,
        >
        >It's possible to check if it's a valid number in <INPUT...
        >
        >I try typeof and parseFloat and it's not working. It's seem impossible to
        >test (with a if) the value "NaN"!![/color]

        if(isNaN(myValu e)){
        alert("That's not a number!");
        }

        <http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/toplev.html#106 4024>

        Comment

        • Dr John Stockton

          #5
          Re: Validate number with JS?

          JRS: In article <nNXhb.43703$Hk 4.1094837@wagne r.videotron.net >, seen in
          news:comp.lang. javascript, Jeff <profcegep@hotm ail.com> posted at Sat,
          11 Oct 2003 14:18:47 :-
          [color=blue]
          >It's possible to check if it's a valid number in <INPUT...
          >
          >I try typeof and parseFloat and it's not working. It's seem impossible to
          >test (with a if) the value "NaN"!![/color]

          There is another trap with isNaN(). AIUI, it accepts a too-big number
          which means infinity. Infinity is unlikely to be valid in most
          applications.

          Is the number to be integer? positive? non-negative? if 22 is allowed,
          do you need also to accept 2.2e1? How about 022 - is it allowed, and is
          its value twenty-two or eighteen? How about 0XED for 237? - or, indeed,
          CCXXXVII? If 022 is allowed, hoe about 000000000000000 000000000000022 ?

          For the most probable answers to those questions, you would do well to
          check the string that <INPUT... "has" in .value with a RegExp. When the
          test is passed, convert to number with a unary +, as in the FAQ, 4.21;
          and then, if necessary, test the numeric value to be within range.

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