Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Stan Weiss Newsgroups: comp.lang.javascript Subject: Re: How to Pass variable name to a function Date: Wed, 11 Apr 2012 15:16:42 -0400 Organization: Aioe.org NNTP Server Lines: 40 Message-ID: <4F85D89A.4686FE4D@erols.com> References: <4F85B0AE.A1E9FFD8@erols.com> <4F85CB9C.BB893EA6@erols.com> NNTP-Posting-Host: 8p4YCYZc25dk5/DIdgWSVw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org X-Mailer: Mozilla 4.8 [en] (Windows NT 5.0; U) X-Accept-Language: en X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.javascript:13339 Stefan Weiss wrote: > > On 2012-04-11 20:21, Stan Weiss wrote: > > I want it to show any error if the field is not all numeric. > > > > So I changed > > if (isNaN(num)) { > > to > > if( isNaN( field ) ) > > I couldn't resist adding something to this thread, considering our > similarity in names... so I'll mention that isNaN() is perhaps not > exactly what you're looking for: > > isNaN("0x123") > isNaN(".123") > isNaN("2e6") > isNaN("Infinity") > > These will all evaluate to false, meaning these values would pass your > test. If you want to mandate a specific numeric format, your best bet > would be a regular expression. > > For example, > > if (/^[+-]?\d+(?:\.\d+)?$/.test(field)) { > // correct format > } > > would accept input like "123", "123.123", "+123.123", "-123.123", but > reject the values listed earlier. You may also want to check for > minimum/maximum allowed values. > > - stefan Thanks Stefan, while I know it does not catch all errors, I did what to catch things like 3>3 which should have been 3.3. Best Regards, Stan