This is crazy:
Code:
function validInt(value)
{
for (var i=0; i < value.length; i++) {
var c = value.charAt(i);
if ((c < '0' || c > '9') && c != '.') {
alert("There's a problem: all values herein must " +
"be numbers only, and " + value +
" has an unacceptable character " + "'" + c + "'");
return(false);
}
}
return(true);
}
validInt??? Integers do *NOT* have decimal points, so what in the world is the test for period doing in there?
On top of that, as written that code would *ALLOW* input such as ".........." or "3........9" or "3.999999999999", none of which make a lot of sense.
If you want to check for a valid number--and here I will assume it is to be no more than 2 digits after the decimal point (i.e., dollars and cents) then:
Code:
function validNum( value )
{
if ( ( /^\-?(\.\d\d?|\d+(\.\d?\d?)?)$/ ).test( value ) { return true; }
document.getElementById("errorMessage") =
"That is not a valid dollars and cents value";
return false;
}
The
\-? in red is optional, if you want to allow negative numbers.
Notice that this also avoids the use of
alert( ), which is obsolete.
But if you aren't so fussy--if you *WILL* allow numbers such as 3.9999999 or 13E2--then let JS test for you:
Code:
if ( isNaN( value ) ) { ... not a valid number ... } else { ... valid ... }