Testing for Numeric Data

Discussion in 'Javascript' started by Cogito, Jul 8, 2004.

  1. Cogito

    Cogito Guest

    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.
    Cogito, Jul 8, 2004
    #1
    1. Advertising

  2. Cogito

    Erwin Moller Guest

    Cogito wrote:

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


    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
    Erwin Moller, Jul 8, 2004
    #2
    1. Advertising

  3. Cogito

    Selrak Guest

    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"
    <> escribió en
    el mensaje news:40ed3f03$1$568$4all.nl...
    > Cogito wrote:
    >
    > > 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.

    >
    > 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
    Selrak, Jul 8, 2004
    #3
  4. Cogito

    Randy Webb Guest

    Selrak wrote:
    > 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.


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

    http://www.jibbering.com/faq/#FAQ4_12

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/
    Randy Webb, Jul 8, 2004
    #4
  5. JRS: In article <40ed3f03$1$568$4all.nl>, seen in
    news:comp.lang.javascript, Erwin Moller <since_humans_read_this_I_am_spa
    > posted at Thu, 8 Jul 2004 14:33:11 :
    >Cogito wrote:
    >
    >> 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.


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


    But parseInt(S, 10) will accept "10 bananas", which is not a decimal
    number. It also accepts 1.999 as meaning 1.

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


    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.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jul 8, 2004
    #5
  6. Cogito

    Lee Guest

    Erwin Moller said:
    >
    >Cogito wrote:
    >
    >> 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.

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


    That depends on what you consider to be "the conversion".
    parseInt("action",16) will quite contentedly convert that
    invalid input into the number 172.
    Lee, Jul 9, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    933
    X-Centric
    Jun 30, 2005
  2. darrel
    Replies:
    4
    Views:
    817
    darrel
    Jul 19, 2007
  3. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    959
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  4. Mike Copeland

    Testing for Numeric Digits

    Mike Copeland, Feb 26, 2012, in forum: C++
    Replies:
    11
    Views:
    511
    Quint Rankid
    Feb 28, 2012
  5. Replies:
    2
    Views:
    393
Loading...

Share This Page