tags:

views:

60

answers:

2

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?

+1  A: 
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" />
Darin Dimitrov
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.
Sam
Sam
just add else condition like this after the if `else{ alert("Please enter integer values for width and height")}`
sandeepan
this will be better - `if (!isWidthAnInteger }else if(!isWidthAnInteger){alert("Please enter integer value for width");}elseif(!isHeightAnInteger){alert("Please enter integer value for height");}else{// TODO: window.open}`
sandeepan
A: 

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.

Zafer