473,811 Members | 3,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(expre ssion) {

var nums = "0123456789 ";

if (expression.len gth==0)return(f alse);

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

if(nums.indexOf (expression.cha rAt(n))==-1)return(false) ;
}

return(true);

}
Jul 20 '05 #1
4 42393
One glaringly easier approach would be just to do something like this:
either
if (isNaN(expressi on))return false;
or
if (isNaN(parseInt (expression)))r eturn false;
or (assuming floating-point numbers are possible in your expression)
if (isNaN(parseFlo at(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***@deloachc orp.com> wrote in message
news:jv******** ************@ea tel.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(expre ssion) {

var nums = "0123456789 ";

if (expression.len gth==0)return(f alse);

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

if(nums.indexOf (expression.cha rAt(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*****@hotmai l.com> wrote in message
news:44******** *************** *********@4ax.c om...
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(expressi on))return false;
or
if (isNaN(parseInt (expression)))r eturn false;
or (assuming floating-point numbers are possible in your expression)
if (isNaN(parseFlo at(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***@deloachc orp.com> wrote in message
news:jv******* *************@e atel.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(expre ssion) {

var nums = "0123456789 ";

if (expression.len gth==0)return(f alse);

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

if(nums.indexOf (expression.cha rAt(n))==-1)return(false) ;
}

return(true);

}

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

isn't a very good test because it allows bad things through. For
example, parseInt("40lak jsdlfj") 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(expre ssion)
{
return (String(express ion).search(/^\d+$/) != -1);
}

Regards,
Steve

Jul 20 '05 #3
Steve van Dongen <st*****@hotmai l.com> writes:
I just wanted to note that
if (isNaN(parseInt (expression)))r eturn false; isn't a very good test because it allows bad things through. For
example, parseInt("40lak jsdlfj") 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(express ion).search(/^\d+$/) != -1);


You can make this a little shorter:
return String(expressi on).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***@deloachc orp.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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.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
1236
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 >>> MLab.std(Numeric.array(, Numeric.Int0)) 0.0 >>> MLab.std(Numeric.array(, Numeric.UnsignedInt8)) 181.01933598375618
0
1421
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. However, Numeric is currently used in a large installed base. In particular SciPy uses Numeric as its core array. While no doubt numarray arrays will be supported in the future, the speed of the less bulky Numeric arrays and the typical case...
2
1835
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 workstation cluster with 64-Bit Opteron CPUs - I have no more than three days to do this. Compiling Python and running Numeric has been no problem at all. However, I get an error message when accessing the data pickled before. (I can load it...
4
3955
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 want it to do is like this. example:
6
9234
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 archives (http://archives.postgresql.org/pgsql-admin/2000-05/msg00032.php), but since it was quite old I'm posting my question anyway. I'm writing a generic database layer that should support a fixed number of generic numeric types on a number of...
10
2254
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 re-implementation of an older Python array module called Numeric" http://www.stsci.edu/resources/software_hardware/numarray
7
1669
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 array with mutiple occurrences of the values in range_va #sat_id is an (600x600) Numeric array with values ranging from -2 to 2
0
2002
by: robert | last post by:
just a note - some speed comparisons : 0.60627370238398726 0.42836673376223189 0.36965815487747022 0.016557970357098384 0.15692469294117473 0.01951756438393204
15
1906
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 PIL-1.1.5.win32-py2.4.exe Numeric-24.2.win32-py2.4.exe Wayne T. Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
13
14988
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
9604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10394
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10127
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.