About

Labels

slider

Recent

Navigation

Detect NaN in Javascript

Detect NaN in Javascript

NaN is returned when a text field value in not a number. But to detect it, you can't write an if condition that variable x is equals to NaN because NaN is not equal to itself, so what you going to use is a an universal function.

Javascript well, have a solution for everything and this one too. Javascript have an universal function that isNan() which returns the value in the form of boolean. You check weather the return value is true or false and add your condition according to that.

But yes with this universal function we are using if condition.
var x=document.getElementById('foo').value;
if(isNaN(x)==true){
// Do Something
}

Type number or string and see what you get.
Share
Banner

Muhammad Hamza

Themelet provides the best in market today. We work hard to make the clean, modern and SEO friendly blogger templates.

Post A Comment:

0 comments: