testing if user-entered string is an integer
want to test it is not a float, not a string, and not beyond 2**32, because that is limit of javascrpt's integer arithmetic
by hrabinowitz
HTML
<form>
<input type="text" id="n1"></input>
<input type="button" id="go" value="GO"></input>
</form>
<p>result is <span id="out1"></span>
</p>
JavaScript
(function () {
function isInteger(n) {
// note: n|0 does conversion to a signed 32 bit integer, then or's with 0,
// so if n is not representable as a 32 bit integer, the test n===(n|0) will fail.
return n === +n && n === (n | 0);
}
$('#go').click(function () {
var theText = $('#n1').val();
console.log("click(): theText=", theText, "parseInt(theText)=", parseInt(theText));
$('#out1').text(isInteger(parseInt(theText)));
});
})();