Computer >> Computer tutorials >  >> Programming >> Javascript

How to validate a given number in JavaScript?


To validate a given number, use the isNan() function. It checks whether a value is a number of not. If true returns, it means the value is not a number.

Example

The following code returns false, which means the value is a number −

<!DOCTYPE html>
<html>
   <body>
   <button onclick="display()">Check</button>
   <p id="test"></p>
      <script>
         function display() {
            var a = "";
            a = a + isNaN(6234) + ": 6234<br>";
            a = a + isNaN(-52.1) + ": -52.1<br>";
            a = a + "The above values return false, i.e. numbers.";

            document.getElementById("test").innerHTML = a;
         }
      </script>
   </body>
</html>