473,699 Members | 2,702 Online

# isInteger, isFloat functions?

Hello,
Does anyone have handy functions for testing whether a text field value is

a) a valid integer
b) a valid floating point number?

Thanks for any assistance, - Dave
Jul 23 '05 #1
35 32864
Hello,
Does anyone have handy functions for testing whether a text field value is

a) a valid integer
b) a valid floating point number?

Try the FAQ:

http://www.jibbering.com/faq/#FAQ4_12

- poison MS or Netscape. I think the Netscape reference is
better on this one. You may find isNaN useful too.

Cheers, Rob.
Jul 23 '05 #2

RobG wrote:
Hello,
Does anyone have handy functions for testing whether a text field
value is
a) a valid integer
b) a valid floating point number?

Try the FAQ:

http://www.jibbering.com/faq/#FAQ4_12

- poison MS or Netscape. I think the Netscape reference is
better on this one. You may find isNaN useful too.

Cheers, Rob.

parseInt and parseFloat will both accept non-numeric characters in a string and
return the digits used up to the first non-digit character -- e.g.:

parseInt("123ab c") will return 123 [a "true"]
parseFloat("123 .2abc") will return 123.2 [also a "true"]

technically, you cannot pass either of these to an "isInteger" and "isFloat" and
receive a true -- they should both be false.
Jul 23 '05 #3

Hello,
Does anyone have handy functions for testing whether a text field value is

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

}
b) a valid floating point number?

function
isFloat(s)
{
var n = trim(s);
return n.length>0 && !(/[^0-9.]/).test(n) && (/\.\d/).test(n);
}
isFloat "qualifies" and differentiates from an integer by testing for a decimal
point and at least one digit after it. Therefore, 123. will not qualify as a
floating point number [nor an integer in isInteger] -- it is an invalid input.
If you require a digit *before* the decimal point for a float, you'll have to

These routines merely test for *any* non-digit(/non-numeric) character
(therefore the 'g' selector in the regex is not required).
[note: empty strings will return a "false positive" - that's why testing for
string length]

To trim away any spaces before or after the string:

function
trim(s)
{
return s.replace(/^\s+|\s+\$/g, "");
}
You can test for Integer OR Float with:

function
isNumber(s)
{
var n = trim(s);
return n.length>0 && +n == n;
}

or you can use !isNaN(numstr) on a non-empty string. [!isNaN("") returns true
and isNaN apparently trims strings of whitespace before testing]
Jul 23 '05 #4
D. Alvarado wrote on 20 sep 2004 in comp.lang.javas cript:
Does anyone have handy functions for testing whether a text field
value is

a) a valid integer
t = " -12.345"
t = " -12345"
b) a valid floating point number?

t = " -12.34.5"
t = " -12.345"

Shoot!
--
Evertjan.
The Netherlands.
but let us keep the discussions in the newsgroup)

Jul 23 '05 #5
On 20/9/04 10:54 am, Evertjan. wrote:
D. Alvarado wrote on 20 sep 2004 in comp.lang.javas cript:
Does anyone have handy functions for testing whether a text field
value is

a) a valid integer

t = " -12.345"
t = " -12345"
b) a valid floating point number?

t = " -12.34.5"
t = " -12.345"

Shoot!

<pedant>
</pedant>

--
Philip Ronan
ph***********@v irgin.net
Jul 23 '05 #6
Philip Ronan wrote on 20 sep 2004 in comp.lang.javas cript:
On 20/9/04 10:54 am, Evertjan. wrote:
a) a valid integer
t = " -12.345"
t = " -12345"

[..]
<pedant>
</pedant>

t = "1.2e+03"
t = "5.0000"

will take care of those.

However, it will miserably fail here:

t = "1.2e+33"

Though technically incorrect [John?],
are such big/small numbers important to the OP ?
--
Evertjan.
The Netherlands.
but let us keep the discussions in the newsgroup)

Jul 23 '05 #7
On 20/9/04 11:15 am, Evertjan. wrote:
t = "1.2e+03"
t = "5.0000"

will take care of those.

However, it will miserably fail here:

t = "1.2e+33"

It will also fail when t = infinity (e.g., "t=1/0")

--
Philip Ronan
ph***********@v irgin.net
Jul 23 '05 #8
Fox wrote:

technically, you cannot pass either of these to an "isInteger" and
"isFloat" and receive a true -- they should both be false.

I think Rob is on the right track (but could have offered some
code...)

Testing strings using regular expressions to see if they have
decimal places (and how many), surplus characters, scientific
notation, etc. requires exhaustive testing... let JS do
the work.

var x = someValue;
if (x == parseInt(x) && x == parseFloat(x)) {
alert(x + ' is a int');
} else if (x == parseFloat(x)) {
alert(x + ' is a float');
} else {
alert(x + ' is not a number I like...');
}

You can even use scientific notation if you like, so 1.2e+03 is
recognised as a float quite happily without complex pattern
matching.

Cheers, Fred
Jul 23 '05 #9
Philip Ronan wrote on 20 sep 2004 in comp.lang.javas cript:
It will also fail when t = infinity (e.g., "t=1/0")

I don't think infinity is an integer but I wouldn't mind if it is.

(+t+1)!=+t ?

--
Evertjan.
The Netherlands.