Curtis wrote:
Quote:
bizt wrote:
Quote:
>I am using the following function to validate a forms value as an
>integer
>>
>function isNumeric(str){
> var numericExpression = /^[0-9]+$/;
> if(str.match(numericExpression)){
> return true;
> }else{
> return false;
> }
>}
>>
>I need one to validate a forms value as a float number. I understand
>that it should just simply be a change to the value of the
>numericExpression variable but Im not too good with expression. Ive
>tried doing some searches on google but either couldnt find the
>relevant function or they just didnt work. Can anyone help? Thanks
>
Technically, if something is numeric, it shouldn't matter if it's a
float or not. However, you might try:
>
function isInteger(num) {
if ( !isNaN(num) ) {
return parseInt(num) == parseFloat(num) ? true : false;
}
else {
return false;
}
}
That is not going to provide a correct result. Remember that parseInt()
without second argument parses the string based on its prefix: "0x" as
hexadecimal, "0" as octal (implementation-dependent; JavaScript 1.8 still
does it, though), other numerics as decimal, and non-numeric as NaN -- which
is the reason why we usually recommend parseInt(..., 10) in scripts.
parseFloat(), on the other hand, parses every value as a decimal value and
returns `NaN' if it cannot be interpreted as such. So for example
!isNaN("0123")
evaluates to `true', but
parseInt("0123") == parseFloat("0123")
evaluates to `false' (because 83 != 123), although the value clearly can be
considered an integer.
And finally, the `==' operation results in a boolean value already. The
conditional operation is superfluous and inefficient in such a case; that
would seem to apply for all programming languages that have it.
Quote:
function isFloat(num) {
if ( !isNaN(num) ) {
if ( /\.0+$/.test(num) ) {
return true;
}
else {
return parseInt(num) != parseFloat(num) ? true : false;
}
}
else {
return false;
}
}
Obvious by now, this test is equally flawed. ISTM it can be replaced safely
with
function getDecimals(num)
{
return num % 1;
}
While the return value is of type `number' and not of type `boolean' here,
it suffices for implicit type conversion later. Incidentally, it does not
make much sense to test an ECMAScript Number value for being a
floating-point value, because *all* ECMAScript Number values are IEEE-754
double-precision [64-bit] floating-point values.
Quote:
Guess I couldn't entirely eliminate regex.
Guess you haven't RTFM, the FAQ, or the Specification.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16