Connecting Tech Pros Worldwide Help | Site Map

Is numeric?

  #1  
Old July 20th, 2005, 10:23 AM
Aaron DeLoach
Guest
 
Posts: n/a
I may have cross-posted this... :-(

I'm a VB programmer getting *way* to deep into this wonderful new JS
venture. I looked around for a function like VBs' IsNumeric without much
success. I had to roll my own. Does anyone see any bugs in this?

function IsNumeric(expression) {

var nums = "0123456789";

if (expression.length==0)return(false);

for (var n=0; n < expression.length; n++){

if(nums.indexOf(expression.charAt(n))==-1)return(false);


}

return(true);

}


  #2  
Old July 20th, 2005, 10:23 AM
Mark
Guest
 
Posts: n/a

re: Is numeric?


One glaringly easier approach would be just to do something like this:
either
if (isNaN(expression))return false;
or
if (isNaN(parseInt(expression)))return false;
or (assuming floating-point numbers are possible in your expression)
if (isNaN(parseFloat(expression)))return false;

One of those should fit what you're looking for. For more info on isNaN(),
just do a google search. Hell, if you use isNaN(), you don't need your
custom function in the first place. It's built in (just opposite of VB).

Good luck on your javascript adventures,
Matt

"Aaron DeLoach" <aaron@deloachcorp.com> wrote in message
news:jvqdnWezaagNe7iiU-KYvA@eatel.net...[color=blue]
> I may have cross-posted this... :-(
>
> I'm a VB programmer getting *way* to deep into this wonderful new JS
> venture. I looked around for a function like VBs' IsNumeric without much
> success. I had to roll my own. Does anyone see any bugs in this?
>
> function IsNumeric(expression) {
>
> var nums = "0123456789";
>
> if (expression.length==0)return(false);
>
> for (var n=0; n < expression.length; n++){
>
> if(nums.indexOf(expression.charAt(n))==-1)return(false);
>
>
> }
>
> return(true);
>
> }
>
>[/color]


  #3  
Old July 20th, 2005, 10:23 AM
Aaron DeLoach
Guest
 
Posts: n/a

re: Is numeric?


I tried the isNaN() with unsatisfactory results. I don't quite understand
*how* it works but your post chopped the code from eight lines to three (I
like efficient code). I'll have to read the M$ link you provided. Thanks
for your help.

"Steve van Dongen" <stevevd@hotmail.com> wrote in message
news:441civkqcbi7mqh58gjbcmktfa3cpeanst@4ax.com...[color=blue]
> On Mon, 28 Jul 2003 23:13:42 -0600, "Mark" <bigmw@charter.net> wrote:
>[color=green]
> >One glaringly easier approach would be just to do something like this:
> >either
> >if (isNaN(expression))return false;
> >or
> >if (isNaN(parseInt(expression)))return false;
> >or (assuming floating-point numbers are possible in your expression)
> >if (isNaN(parseFloat(expression)))return false;
> >
> >One of those should fit what you're looking for. For more info on[/color][/color]
isNaN(),[color=blue][color=green]
> >just do a google search. Hell, if you use isNaN(), you don't need your
> >custom function in the first place. It's built in (just opposite of VB).
> >
> >Good luck on your javascript adventures,
> >Matt
> >
> >"Aaron DeLoach" <aaron@deloachcorp.com> wrote in message
> >news:jvqdnWezaagNe7iiU-KYvA@eatel.net...[color=darkred]
> >> I may have cross-posted this... :-(
> >>
> >> I'm a VB programmer getting *way* to deep into this wonderful new JS
> >> venture. I looked around for a function like VBs' IsNumeric without[/color][/color][/color]
much[color=blue][color=green][color=darkred]
> >> success. I had to roll my own. Does anyone see any bugs in this?
> >>
> >> function IsNumeric(expression) {
> >>
> >> var nums = "0123456789";
> >>
> >> if (expression.length==0)return(false);
> >>
> >> for (var n=0; n < expression.length; n++){
> >>
> >> if(nums.indexOf(expression.charAt(n))==-1)return(false);
> >>
> >>
> >> }
> >>
> >> return(true);
> >>
> >> }[/color][/color]
>
> I just wanted to note that[color=green]
> >if (isNaN(parseInt(expression)))return false;[/color]
> isn't a very good test because it allows bad things through. For
> example, parseInt("40lakjsdlfj") is 40, which is a number, but the
> original input string (obviously) isn't. It also blocks valid inputs
> like "09" which is a valid decimal number but not a valid octal
> number.
>
> Aaron, that function of yours should work fine. You could also use a
> regular expression.
>[/color]
http://msdn.microsoft.com/library/en...gexpsyntax.asp[color=blue]
>
> function IsNumeric(expression)
> {
> return (String(expression).search(/^\d+$/) != -1);
> }
>
> Regards,
> Steve[/color]


  #4  
Old July 20th, 2005, 10:24 AM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a

re: Is numeric?


Steve van Dongen <stevevd@hotmail.com> writes:
[color=blue]
> I just wanted to note that[color=green]
> >if (isNaN(parseInt(expression)))return false;[/color]
> isn't a very good test because it allows bad things through. For
> example, parseInt("40lakjsdlfj") is 40, which is a number, but the
> original input string (obviously) isn't. It also blocks valid inputs
> like "09" which is a valid decimal number but not a valid octal
> number.[/color]

Valid point. Try this instead

function isNumeric(value) {
return typeof value != "boolean" && value !== null && !isNaN(+ value);
}

The prefix "+" converts its argument to a number just like the Number
function.
The extra checks are there because booleans and null can be converted
to the numbers 0 and 1.
[color=blue]
> return (String(expression).search(/^\d+$/) != -1);[/color]

You can make this a little shorter:
return String(expression).match(/^\d+$/);

--
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.'
  #5  
Old July 20th, 2005, 10:24 AM
Dr John Stockton
Guest
 
Posts: n/a

re: Is numeric?


JRS: In article <jvqdnWezaagNe7iiU-KYvA@eatel.net>, seen in
news:comp.lang.javascript, Aaron DeLoach <aaron@deloachcorp.com> posted
at Mon, 28 Jul 2003 22:14:23 :-[color=blue]
>I may have cross-posted this... :-([/color]

You did not; perhaps you mean multi-posted.
[color=blue]
> I looked around for a function like VBs' IsNumeric without much
>success.[/color]

Without an accurate knowledge of the VB function it is difficult to
emulate it reliably. For example. what about an empty string?

function IsNumeric(S) { return S > '' && ! isNaN(S) }

seems reasonable. But, for me, it accepts 1e9999 - Infinity is a
number.

function IsNumeric(S) { return S > '' && isFinite(S) }

However, in any practical application, the permissible input is likely
to be more limited; use a RegExp to test for, say, 1..5 decimal digits
preceded by sign or space.

OK = /^[-+ ]?\d{1,5}$/.test(S)

--
© 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.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
when checking that input is numeric we use a_______ Immature Little Child answers 2 October 3rd, 2008 05:25 PM
I have a isNumeric function to check a text field is numeric .. I nowneed a isFloat bizt answers 9 September 18th, 2008 11:55 PM
how to check given value is numeric or not vinodkus@gmail.com answers 0 February 5th, 2008 10:45 AM
Best way to check if string is numeric tshad answers 16 December 27th, 2007 12:25 AM
How to test if a variable is numeric? Randell D. answers 10 July 23rd, 2005 06:48 PM