473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is numeric?

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);

}
Jul 20 '05 #1
4 42371
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" <aa***@deloachcorp.com> wrote in message
news:jv********************@eatel.net...
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);

}

Jul 20 '05 #2
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" <st*****@hotmail.com> wrote in message
news:44********************************@4ax.com...
On Mon, 28 Jul 2003 23:13:42 -0600, "Mark" <bi***@charter.net> wrote:
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" <aa***@deloachcorp.com> wrote in message
news:jv********************@eatel.net...
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);

}

I just wanted to note that
if (isNaN(parseInt(expression)))return false;

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.

http://msdn.microsoft.com/library/en...gexpsyntax.asp
function IsNumeric(expression)
{
return (String(expression).search(/^\d+$/) != -1);
}

Regards,
Steve

Jul 20 '05 #3
Steve van Dongen <st*****@hotmail.com> writes:
I just wanted to note that
if (isNaN(parseInt(expression)))return false; 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.


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.
return (String(expression).search(/^\d+$/) != -1);


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

--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
JRS: In article <jv********************@eatel.net>, seen in
news:comp.lang.javascript, Aaron DeLoach <aa***@deloachcorp.com> posted
at Mon, 28 Jul 2003 22:14:23 :-
I may have cross-posted this... :-(
You did not; perhaps you mean multi-posted.
I looked around for a function like VBs' IsNumeric without much
success.


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.
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kyler Laird | last post by:
Python 2.3.3 (#2, Jan 4 2004, 12:24:16) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Numeric >>> import MLab >>> import Scientific.Statistics...
0
by: Travis Oliphant | last post by:
Numarray is making great progress and is quite usable for many purposes. An idea that was championed by some is that the Numeric code base would stay static and be replaced entirely by Numarray. ...
2
by: Johannes Nix |Johannes.Nix | last post by:
Hi, I have a tricky problem with Numeric. Some time ago, I have generated a huge and complex data structure, and stored it using the cPickle module. Now I want to evaluate it quickly again on a...
4
by: Gezeala 'Eyah' Bacu\361o II | last post by:
hey guys..need your help on this.. i have a plpgsql function where in i compute numeric values for my php scripts.. my problem is my function just won't round some numbers properly.. what i...
6
by: M.A. Oude Kotte | last post by:
Hi All, I hope this is the correct mailing list for this question. But neither postgresql.org nor google could help me out on this subject. I did find one disturbing topic on the mailing list...
10
by: Bryan | last post by:
hi, what is the difference among numeric, numpy and numarray? i'm going to start using matplotlib soon and i'm not sure which one i should use. this page says, "Numarray is a...
7
by: Sheldon | last post by:
Hi, I have the following loop that I think can be written to run faster in Numeric. I am currently using Numeric. range_va = main.xsize= 600 main.ysize= 600 #msgva is an (600x600) Numeric...
0
by: robert | last post by:
just a note - some speed comparisons : 0.60627370238398726 0.42836673376223189 0.36965815487747022 0.016557970357098384 0.15692469294117473 0.01951756438393204
15
by: W. Watson | last post by:
For some reason Python 2.2.4 cannot find the Numeric module. It's been suggested that I should re-install the Numeric file. How do that? Also the PIL. The three install files are: python-2.4.4.msi...
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.