• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

function in javascript to check data is numeric

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the function in javascript to check whether the given data is numeric or not?
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you mean exactly?

do you mean you want to see if what you have is a number? if that is the case..there isnt one that I know of...

you should google for th java scripts API...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We've got a JavaScript forum here. I'll move this there for you.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what you are looking for is: isNaN

Eric
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All you need is something like this



or simply


[ February 20, 2005: Message edited by: Eugene Lucash ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using is not the best way to check for being numeric, since it allows trailing garbage in the argument (e.g. would yield 2.2).

There is also no reason to do a . You already have a boolean, so a simple negation would suffice (pet peeve, sorry .

When converting a string to a number, I recommend using the function. A (very slightly) more efficient method is to use the prefix plus operator. More on converting to numbers can be found in the comp.lang.javascript FAQ (it's a great FAQ, read it all!).

So:


That accepts all strings that are number literals in Javascript. Often when someone wants data to be numeric, they have a specific format in mind. If the format is simple, using a regular expression on the string might be more efficient than converting it to a number that isn't used anyway. E.g., to check whether a string is an integer numeral:



Regards
/L
 
Eugene Lucash
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, Lasse, you had pointed better solution
[ February 23, 2005: Message edited by: Eugene Lucash ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic