Checking if a variable is an integer in javascript
Asked Answered
C

3

5

I made a form where the user inputs values for width and height that they want for the pop up window to be. I am using window.open for that.

So I think I need to check if the values for width and height are integer. I have a function that checks that a variable is an integer that is...

function isInteger(possibleInteger) {
    return !isNaN(parseInt(possibleInteger));
}

but I don't know how to call this function to the width and height function to check if the user inputted an integer. Can any one help?

Conative answered 5/9, 2010 at 9:1 Comment(0)
S
6

This is an answer to question mentioned in the topic, not the actual one in the body of the text :).

The following method is more accurate on determining if the string is a real integer.

function isInteger(possibleInteger) {
    return /^[\d]+$/.test(possibleInteger)​;
}

Your current method validates "7.5" for instance.

EDIT: Based on machineghost's comment, I fixed the function to correctly handle arrays. The new function is as follows:

function isInteger(possibleInteger) {
        return Object.prototype.toString.call(possibleInteger) !== "[object Array]" && /^[\d]+$/.test(possibleInteger);
}
Silicate answered 5/9, 2010 at 10:50 Comment(1)
/^[\d]+$/.test([1]) returns true, and [1] is definitely an array, not an integerSybarite
L
4

An alternative answer if you worry about performance.

var isInteger1 = function(a) {
    return ((typeof a !== 'number') || (a % 1 !== 0)) ? false : true;
};

Load test results compared to Zafer's answer in Chrome:

undefined => 4ms vs 151ms
1 => 10ms vs 390ms
1.1 => 61ms vs 250ms
'1' => 8ms vs 334ms
[1] => 9ms vs 210ms
{foo: 'bar'} => 8ms vs 478ms

See for yourself: jsfiddle

Liva answered 29/4, 2013 at 17:5 Comment(0)
J
1
var isWidthAnInteger = isInteger(document.getElementById('width').value);
var isHeightAnInteger = isInteger(document.getElementById('height').value);
if (isWidthAnInteger && isHeightAnInteger) {
    // TODO: window.open
}

where you have the following textboxes:

Width: <input type="text" id="width" name="width" />
Height: <input type="text" id="height" name="height" />
Joycelynjoye answered 5/9, 2010 at 9:6 Comment(4)
ok cool thanks! Do you know if i could do what u did but do if and else statements because i also want to check if it is an int and if it is not i want to send an error message back to the user. Thanks for ur help sorry new to javascript.Conative
oh i am checking if width and height is int in a function and my window.open is another function called page load will this effect if (isWidthAnInteger && isHeightAnInteger) { // TODO: window.open } ???Conative
just add else condition like this after the if else{ alert("Please enter integer values for width and height")}Holliehollifield
this will be better - if (!isWidthAnInteger && !isHeightAnInteger){alert("Please enter integer values for width and height");}else if(!isWidthAnInteger){alert("Please enter integer value for width");}elseif(!isHeightAnInteger){alert("Please enter integer value for height");}else{// TODO: window.open}Holliehollifield

© 2022 - 2024 — McMap. All rights reserved.