Testing for Numeric Data

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

    #1

    Testing for Numeric Data

    In a form I have two text fields. In one of them the user enters a
    decimal number and in the other a hexadecimal number. What code can be
    used to validate that the entered text is numeric and to produce a
    warning when it is not.
  • Erwin Moller

    #2
    Re: Testing for Numeric Data

    Cogito wrote:
    [color=blue]
    > In a form I have two text fields. In one of them the user enters a
    > decimal number and in the other a hexadecimal number. What code can be
    > used to validate that the entered text is numeric and to produce a
    > warning when it is not.[/color]

    Hi Cogito ,


    have a look at parseInt(s, radix)
    where s is the string to be parsed and radix is optional, but you should use
    it to test for hexadecimal strings.

    remember that the function parseInt will return NaN (Not A Number) when the
    conversion cannot be done.

    Good luck.

    Regards,
    Erwin Moller

    Comment

    • Selrak

      #3
      Re: Testing for Numeric Data

      Be careful with parseInt, in IE it have a bug, and for 08 or 09 don't have a
      good response. If you can, use parseFloat.

      Selrak


      "Erwin Moller"
      <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> escribió en
      el mensaje news:40ed3f03$1 $568$e4fe514c@n ews.xs4all.nl.. .[color=blue]
      > Cogito wrote:
      >[color=green]
      > > In a form I have two text fields. In one of them the user enters a
      > > decimal number and in the other a hexadecimal number. What code can be
      > > used to validate that the entered text is numeric and to produce a
      > > warning when it is not.[/color]
      >
      > Hi Cogito ,
      >
      >
      > have a look at parseInt(s, radix)
      > where s is the string to be parsed and radix is optional, but you should[/color]
      use[color=blue]
      > it to test for hexadecimal strings.
      >
      > remember that the function parseInt will return NaN (Not A Number) when[/color]
      the[color=blue]
      > conversion cannot be done.
      >
      > Good luck.
      >
      > Regards,
      > Erwin Moller[/color]


      Comment

      • Randy Webb

        #4
        Re: Testing for Numeric Data

        Selrak wrote:[color=blue]
        > Be careful with parseInt, in IE it have a bug, and for 08 or 09 don't have a
        > good response. If you can, use parseFloat.[/color]

        IE doesn't have a "bug" in parseInt, its a bug in the implementation of
        the people using it.



        --
        Randy
        Chance Favors The Prepared Mind
        comp.lang.javas cript FAQ - http://jibbering.com/faq/

        Comment

        • Dr John Stockton

          #5
          Re: Testing for Numeric Data

          JRS: In article <40ed3f03$1$568 $e4fe514c@news. xs4all.nl>, seen in
          news:comp.lang. javascript, Erwin Moller <since_humans_r ead_this_I_am_s pa
          mmed_too_much@s pamyourself.com> posted at Thu, 8 Jul 2004 14:33:11 :[color=blue]
          >Cogito wrote:
          >[color=green]
          >> In a form I have two text fields. In one of them the user enters a
          >> decimal number and in the other a hexadecimal number. What code can be
          >> used to validate that the entered text is numeric and to produce a
          >> warning when it is not.[/color][/color]
          [color=blue]
          >have a look at parseInt(s, radix)
          >where s is the string to be parsed and radix is optional, but you should use
          >it to test for hexadecimal strings.[/color]

          But parseInt(S, 10) will accept "10 bananas", which is not a decimal
          number. It also accepts 1.999 as meaning 1.
          [color=blue]
          >remember that the function parseInt will return NaN (Not A Number) when the
          >conversion cannot be done.[/color]

          Input validation is best done with a RegExp; see <URL:http://www.merlyn.
          demon.co.uk/js-valid.htm>.

          decimal: /^\d+$/
          hexadecimal: /^[0-9a-z]+$/i

          Use {m,n} where m & n are integers to control the number of digits.

          In evaluating the hex string, you may need to prefix it with "0x".

          Remember : the aim of the authors of javascript was to attach some
          meaning to as many forms of input as possible (dates being a good
          example); the aim of the programmer needs to be to ensure that only
          unambiguous input of suitable value is accepted, and to interpret it
          correctly.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> JL / RC : 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

          • Lee

            #6
            Re: Testing for Numeric Data

            Erwin Moller said:[color=blue]
            >
            >Cogito wrote:
            >[color=green]
            >> In a form I have two text fields. In one of them the user enters a
            >> decimal number and in the other a hexadecimal number. What code can be
            >> used to validate that the entered text is numeric and to produce a
            >> warning when it is not.[/color]
            >
            >Hi Cogito ,
            >
            >
            >have a look at parseInt(s, radix)
            >where s is the string to be parsed and radix is optional, but you should use
            >it to test for hexadecimal strings.
            >
            >remember that the function parseInt will return NaN (Not A Number) when the
            >conversion cannot be done.[/color]

            That depends on what you consider to be "the conversion".
            parseInt("actio n",16) will quite contentedly convert that
            invalid input into the number 172.

            Comment

            Working...