473,397 Members | 2,099 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,397 software developers and data experts.

Why is this considered a float?

Hi,

I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic. How would I rewrite the isFloat function so that the
above number fails validation?

if (!(isFloat(price) && parseFloat(price) 0)) {
alert("Price field cannot be empty or zero!");
return;
}

function isFloat(s)
{
if (isInteger(s)) {
return true;
} // if
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.
\d/).test(n);
} // isFloat

function isInteger(s)
{
var n = trim(s);
return n.length 0 && !(/[^0-9]/).test(n);
}

Thanks, - Dave
Dec 3 '07 #1
5 1872
la***********@zipmail.com wrote on 03 dec 2007 in comp.lang.javascript:
Hi,

I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic. How would I rewrite the isFloat function so that the
above number fails validation?

if (!(isFloat(price) && parseFloat(price) 0)) {
alert("Price field cannot be empty or zero!");
return;
}
How do you define a float? Any noninteger number?

function isFloat(s)
{
if (isInteger(s)) {
return true;
} // if
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.
\d/).test(n);
} // isFloat

function isInteger(s)
{
var n = trim(s);
return n.length 0 && !(/[^0-9]/).test(n);
}
Just:

if ( isNaN(price) || !price )
alert("Price field cannot be zero or nonnumeric!");

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 3 '07 #2
la***********@zipmail.com wrote:
I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic.
....

return n.length>0 && !(/[^0-9.]/).test(n) && (/\.\d/).test(n);
!(/[^0-9.]/).test(n) tests "Is there no character other than 0-9 and .?"
"29..99" passes this; all the characters are either 0-9 or a period.

(/\.\d/).test(n) tests "Is there a period followed by a digit?"
"29..99" passes this; there is a period followed by a dot.

--
John W. Kennedy
"The pathetic hope that the White House will turn a Caligula into a
Marcus Aurelius is as naïve as the fear that ultimate power inevitably
corrupts."
-- James D. Barber (1930-2004)
Dec 3 '07 #3
la***********@zipmail.com wrote:
I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic.
....

return n.length>0 && !(/[^0-9.]/).test(n) && (/\.\d/).test(n);
!(/[^0-9.]/).test(n) tests "Is there no character other than 0-9 and .?"
"29..99" passes this; all the characters are either 0-9 or a period.

(/\.\d/).test(n) tests "Is there a period followed by a digit?"
"29..99" passes this; there is a period followed by a digit.

--
John W. Kennedy
"The pathetic hope that the White House will turn a Caligula into a
Marcus Aurelius is as naïve as the fear that ultimate power inevitably
corrupts."
-- James D. Barber (1930-2004)
Dec 3 '07 #4
On Dec 4, 8:33 am, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Hi,

I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic. How would I rewrite the isFloat function so that the
above number fails validation?

if (!(isFloat(price) && parseFloat(price) 0)) {
alert("Price field cannot be empty or zero!");
return;
}

function isFloat(s)
{
if (isInteger(s)) {
return true;
} // if
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.
\d/).test(n);
} // isFloat

function isInteger(s)
{
var n = trim(s);
return n.length 0 && !(/[^0-9]/).test(n);
}

Thanks, - Dave
If you want to validate that the user has entered something resembling
money in a particular format using a regular expression, then try:

<URL: http://www.merlyn.demon.co.uk/js-valid.htm >

A very simple RegExp test along lines of what you seem to want is:

function isMoneyFormat(s) {
return /^\d+\.\d\d$/.test(s)
}

Which returns true for a string like 0.00 or 1.23 or 1231.23 and so
on.
--
Rob
Dec 4 '07 #5
In comp.lang.javascript message <164a6bf5-383c-4c10-b02a-300aba0dda11@s1
9g2000prg.googlegroups.com>, Mon, 3 Dec 2007 14:33:31,
"la***********@zipmail.com" <la***********@zipmail.composted:
>I have a text field with the value "29..99" (notice the two dots
instead of one) and it is considered a float according to the
following logic. How would I rewrite the isFloat function so that the
above number fails validation?

if (!(isFloat(price) && parseFloat(price) 0)) {
alert("Price field cannot be empty or zero!");
return;
}
function isPrice(s) { return (/^\s*\d+(\.\d\d)?\s*/$).test(s) && s>0 }
// Negative and exponent not allowed; well-formed by IUPAP/SUNAMCO
// integer euros or cents, as euros

function isFloat(s)
{
if (isInteger(s)) {
return true;
} // if
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.
\d/).test(n);
} // isFloat
function isFloat(s) { return (/^\s*\d+(\.\d+)?\s*$/).test(s) }
// Negative and exponent not allowed; well-formed by IUPAP/SUNAMCO
// allows integer
function isInteger(s)
{
var n = trim(s);
return n.length 0 && !(/[^0-9]/).test(n);
}
function isInteger(s) { return (/^\s*\d+\s*$/).test(s) }
UNDERTESTED. Remember that all ECMA 3 Numbers are IEEE doubles, which
are floats.

<URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 4 '07 #6

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

Similar topics

5
by: Pat | last post by:
Give two double-typed variable X and Y. If (X==Y) is true, then how about the following results: (float(X) > float(Y))? (float(X) < float(Y))? (float(X) >= float(Y))? ( X > float(Y) )? ( X...
13
by: amanayin | last post by:
The program is simple and i know i should use switch instead of if but i done it this way for a reason. But i am not sure if it should work when i was compiling it i was getting warning messages...
20
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
10
by: rob.iverson | last post by:
We ran into this problem and I can't see why the compilers (Sun One Studio 8 and g++ 2.95) think it's an error: ------------------- class A { public: void foo();
8
by: vjnr83 | last post by:
Hi, I have a doubt: what is the difference between float **p and float *p? Thanks in advance, Vijay
38
by: Mark Dickinson | last post by:
I get the following behaviour on Python 2.5 (OS X 10.4.8 on PowerPC, in case it's relevant.) (0.0, 0.0) (-0.0, -0.0) I would have expected y to be -0.0 in the first case, and 0.0 in the...
24
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
2
by: kgeorge2 | last post by:
A library which i use, has an array class class LibArray { public: LibArray():_array(NULL),_len(0); LibArray(int n, float val):_array( new float),_len(n){} /** more complete definition...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...
0
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...
0
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...

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.