如何检查输入是否为数字并相应地调用函数?

时间:2014-03-03 06:19:04

标签: javascript html

我的代码如下。我需要修改它以便检查输入是否为数字。如果它不是数字,则必须显示如下消息“请仅输入nos”。 如果输入是一个数字,那么必须调用函数compare()。我尝试了很多方法,包括使用isNAN但似乎没有任何工作。任何人都可以帮忙吗?          

<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>

3 个答案:

答案 0 :(得分:1)

您可以尝试这样:

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

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

答案 1 :(得分:0)

parseInt必须与enter_num和generated_num一起使用,现在你只对一个人使用。

<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>

答案 2 :(得分:0)

你试过“typeof”吗?

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

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