• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

JavaScript, check if value is integer?

You can write your own function that does that. E.g. there's a builtin typeof that distiguishes numbers from strings, but not integers from floats. You could use that and further check that value.toString doesn't have '.' inside or some such...

Or rip it off from internet, I'm sure it can be found if you google.
 
Last edited:
Code:
function isInteger(value)
{
if (typeof(value)) == "number")
{
return (floor(value)==value);
}
else
{return false;}
}
 
Code:
function isInteger(value)
{
if (typeof(value)) == "number")
{
return (floor(value)==value);
}
else
{return false;}
}

Could be simplified as:

function isInteger(value) {
return (!isNaN(value) && Math.floor(value) === value);
}

Keeping in mind that this is a strict comparison will return true for say 1,2,3,4 but not when it's in string form "1", "2", "3", "4" which form values often are.

If you want to take string equivalents you might need something like:

function isInteger(value) {
return (!isNaN(value) && Math.floor(value) === parseInt(value));
}
 
Back
Top