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.