Skip to content Skip to sidebar Skip to footer

Detect Invalid Number In Prompt

I want to get the number in a prompt var pr = prompt('Tile size in pixels?', '150'); if(pr != null){ console.log(pr); if (parseInt(pr) != NaN) {loadImage(parseInt(pr));}

Solution 1:

You can use isNaN()

isNaN(1) == false// trueisNaN("hi there") == true// trueisNaN("606") == false// true// etc etc etc 

Solution 2:

You can Try isNaN to check a number

like this

if(!isNaN(pr)){
  // Valid number
}

you can convert your code like this

!isNaN(pr) ? loadImage(parseInt(pr)) : alert("pick a valid number");

Post a Comment for "Detect Invalid Number In Prompt"