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]