-1

I am trying to validate a text box to enter only numbers and not alphabets using javascript NaN function. But i am not getting the correct output

<html>
<body>


<input type="text" id="demo">

<button onclick="myFunction();">Try it</button>

<script>
function myFunction() 
{

var x =document.getElementById("demo");
   if (isNaN(x))
     {
      alert("hi");
     }
    else
    {
    alert("world");
    }
   }
</script>

</body>
</html>
paramaguru
  • 7
  • 2
  • 6

1 Answers1

0

x in your code will be an HTMLInput object. It will never be a number.

You want to test the value of parseInt(x.value).

David Hedlund
  • 128,221
  • 31
  • 203
  • 222