javascript isNaN() function not working? -
i have function test if prompt input number, so:
function myfunction() { var person = prompt("please enter name", ""); if (person != null) { if(isnan(person)) { document.write("hello " + person + "<br><br>"); } else document.write("you gave me number"); } else { document.write("you didn't answer.<br><br>"); } } but every time enter number keeps outputting hello + number. i've been googling function quite time , doesn't make sense me, seems should work. why person returning true?
nan special value in javascript. isnan check see if value passed equal* special value. if want check if is, say, not stream of numbers, can use regular expression:
if (!/^\d+(\.\d+)?/.exec(person)) { or parse value number , see if converts same string:
var n = parsefloat(person); if (n.tostring() !== person) { *there's reason don't use === it's outside scope of answer.
Comments
Post a Comment