Tuesday, September 12, 2017

parseInt will make integers of decimals without error in JavaScript.

So how do you sanity check for just an integer? Like so:

var bar = parseInt(foo);
if((!bar && bar != 0) || (foo.indexOf(".") >= 0)) {
   alert("It's not an integer!");
} else {
   alert("I feel whole.");
}

No comments:

Post a Comment