user3373697
user3373697

Reputation: 23

How to check if input is a number and call a function accordingly?

I have my code below..I need to modify it so that the input is checked if it is number.If its not a number, then a message has to be displayed as follows " please enter only nos". If input is a number,then function compare() has to be called.I tried many ways including using isNAN but nothing seems to work.Can anyone help here please?

<head>
<title>Guessing Game</title>
 <meta charset = "utf-8">
<script>
<!--

var num_guess=0;
function chkinput()
 {
  if (!isNaN(parseFloat(obj)) && isFinite(obj);) 
  {
compare();
  }
  else
   {

    alert("Please enter only numbers");
    return false;
   }
   }

function compare()
    {
 var generated_num = document.getElementById( "target" );
 var entered_num = document.getElementById( "userguess" );


    if ( parseInt( entered_num.value ) > generated_num.value )
   window.alert("Too high. Try again!");
  else if ( parseInt( entered_num.value ) < generated_num.value )
     window.alert("Too low. Try again!");
 else
  {
     window.alert("Congratulations!You guessed the number!");
  if ( parseInt( num_guess ) < 10 )
     window.alert( "Either you know the secret or"
 + " you got lucky!");
      if ( parseInt( num_guess ) == 10 )
   window.alert( "Ahah! You know the secret!" );
     if ( parseInt( num_guess ) > 10 )
   window.alert(
    "You should be able to do better!" );
    guess.userguess.value ="";
    window.status= "Done";


   random();

   } // end else
   num_guess++;
      } // end function compare

    function random() // function to generate a random no between 1 and 1000
    {
    document.getElementById( "target" ).value =
   Math.floor( 1 + Math.random() * 1000 );
   } // end function random
    // -->
  </script>
    </head>

   <body onload = "random()">
   <form id = "guess" action = "">
   <div>
   Guess a number between 1 and 1000:
  <input type = "text" id = "userguess" /><br />
    <input type = "button" value = "Guess"
   onclick = "chkinput()" />
    <input type = "hidden" id = "target" value = "0" />

   </div>
     </form>

Upvotes: 1

Views: 230

Answers (3)

Dan1121
Dan1121

Reputation: 83

have you tried "typeof"?

if ( typeof entered_num  === "number" ) {..} 
else { //not a number }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172628

You may try like this:

if (!isNaN(parseFloat(obj)) && isFinite(obj);) 
  {
    compare();
  }
 else
  {

    alert("Please enter only numbers");
    return false;
  }

Upvotes: 1

SajithNair
SajithNair

Reputation: 3867

parseInt has to be used with both entered_num and generated_num, right now you are doing only for one.

<head>
<title>Guessing Game</title>
 <meta charset = "utf-8">
<script>
<!--

var num_guess=0;

function compare()
    {
 var generated_num = parseInt(document.getElementById( "target" ).value, 10);
 var entered_num = parseInt(document.getElementById( "userguess" ).value, 10);


    if ( entered_num ) > generated_num)
       window.alert("Too high. Try again!");
  else if (entered_num < generated_num)
     window.alert("Too low. Try again!");
 else
  {
     window.alert("Congratulations!You guessed the number!");
  if (num_guess < 10 )
     window.alert( "Either you know the secret or"
 + " you got lucky!");
      if (num_guess == 10 )
   window.alert( "Ahah! You know the secret!" );
     if (num_guess  > 10 )
   window.alert(
    "You should be able to do better!" );
    guess.userguess.value ="";
    window.status= "Done";


   random();

   } // end else
   num_guess++;
      } // end function compare

    function random() // function to generate a random no between 1 and 1000
    {
    document.getElementById( "target" ).value =
   Math.floor( 1 + Math.random() * 1000 );
   } // end function random
    // -->
  </script>
    </head>

   <body onload = "random()">
   <form id = "guess" action = "">
   <div>
   Guess a number between 1 and 1000:
  <input type = "text" id = "userguess" /><br />
    <input type = "button" value = "Guess"
   onclick = "compare()" />
    <input type = "hidden" id = "target" value = "0" />

   </div>
     </form>

Upvotes: 0

Related Questions