Why is this considered a float?

L

laredotornado

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
 
E

Evertjan.

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!");
 
J

John W. Kennedy

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

John W. Kennedy

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

RobG

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.
 
D

Dr J R Stockton

In comp.lang.javascript message <164a6bf5-383c-4c10-b02a-300aba0dda11@s1
9g2000prg.googlegroups.com>, Mon, 3 Dec 2007 14:33:31,
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top