仅将输入字段限制为数字

时间:2013-04-23 23:19:10

标签: javascript html

我正在为学校编写一个程序,他希望我们执行以下操作:如果在Age字段中输入任何非数字条目,则会出现一个警告框,其中包含两行消息:

您输入的年龄必须仅包含数字。 请重新输入。

然后:如果输入的年龄小于18,那么在包含消息的按钮下面会出现另一个段落

然后:对于18岁及以上的人,剧本必须根据申请人获得大学学位的情况计算并显示工资报价。如果申请人持有学位,那么工资报价将通过将申请人的年龄乘以$ 1000.00来确定。没有学位,乘数将只有600.00美元。当按下确定工资按钮时,脚本将显示包含计算工资报价的额外段落元素,如下面的示例插图所示。

我已经尝试了所有我能想到的东西,以确保在年龄字段中没有输入字母字符,但我似乎无法让它工作。请帮助!

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>


<head>
   <meta http-equiv='Content-Type' content='text/html; charset= utf-8' />
   <title>Form Based JavaScript</title>
   <style type = "text/css">
      h2 {text-align:left;}
      body {background-color:lightblue;}
      .input {font-family:"Courier New", Courier, monospace;}
   </style>   

   <script type = "text/javascript">
      <!--
   function salary()  `enter code here`
      {
        var age;
        var degree;


      if (age != [0-99]
        {
            document.writeln( "The age that you entered must contain only numerals. <br /> Please re-enter it.");

      else if (age < 18)

           document.writeln( " Sorry - you must be 18 or older for this job. <br /> Please reapply in [18 - age] years." );


      else if (age >=18)

           document.writeln( " You qualify! Salary offer [degree * ] dollars. See our staff for an application" );
          }



      }


      // -->
   </script>

<!-- project6.htm Tatiana Saavedra 04/17/2013 -->

</head>

<body>

<h2> Applicant Screening Page </h2>

<hr />

<form id = "myForm" action = "">
   <p>Email:<input type = "email" id = "email"></p>
   <p>Age:<input type = "number" maxlength="3" size="3" id = "age"></p>
   <p><label>Check the box if you hold a college degree: <input type = "checkbox" id = "degree"></label></p>


<br />
<form action="">
 <input type="button" value="Determine Salary" onclick="salary()"/>
</form>

</body>

</html>

3 个答案:

答案 0 :(得分:0)

你有几个错误打开和关闭括号,在任何事情之前检查这个,你的javascript没有加载,因此没有做任何事情,正如已经告诉你的那样,你是在声明变量而不是为它们赋值。我还建议你把javascript放在页面底部

答案 1 :(得分:0)

试试这个:

             function isInteger(n) 
             {
                if (n.indexOf('.') != -1)
                {
                   return false;//do not accept decimal numbers
                }
                return !isNaN(parseInt(n)) && isFinite(n);
             }

parseInt将值解析为整数,如果该值包含字母字符,则返回NaN

isNaN检查结果是否为NaN

isFinite处理n以数字开头但包含caracters的情况。

您不仅使用isFinite的原因是因为如果您尝试isFinite(' '),则返回true。

同样使用parseInt(age)值进行比较,自从比较数字

以来它更好

答案 2 :(得分:0)

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>

这是一个不应该使用的旧版DOCTYPE(它的目的是帮助从10年前开始从HTML转换到XHTML,但这种情况从未发生过)。只需使用:

<?DOCTYPE html>
<html>


<head>
   <meta http-equiv='Content-Type' content='text/html; charset= utf-8' />

请参阅?内容是HTML,作为HTML提供。 :-) [...]

      

如果这是XML,您只需将页面空白。近20年来,HTML注释分隔符并不是必需的,在符合HTML标准的浏览器中,头文件元素从不需要它们。不要使用它们。

function salary()  `enter code here`
  {

哎呀,你不能有字符串文字。把它放在函数体中。我喜欢将左大括号放在与函数关键字和名称相同的行上。我会假设弦不会出现在那里:

function salary() {
    var age;
    var degree;

    if (age != [0-99]

缺少结束括号“)”。

    {
        document.writeln( "The age that you entered must contain only numerals. <br /> Please re-enter it.");

如果您在文档加载完成后调用document.writeln,则会先调用document.open

清除文档中的所有内容。你可能想要做的是在文档中添加一个元素并将文本放在其中。或者将文本放在页面中已有的元素中。

    else if (age < 18)

你应该在所有if块周围使用大括号,它使代码更容易阅读。对于非常简单的块和语句,有时省略它们很方便,但不是这里。小号

       document.writeln( " Sorry - you must be 18 or older for this job. <br /> Please reapply in [18 - age] years." );

你似乎试图在字符串中进行算术,你不能这样做。因此,在写入元素时替换document.writeln,并执行arithemtic,如:

"Please reapply in " + (18 - age) + " years."

    else if (age >=18)
        document.writeln( " You qualify! Salary offer [degree * ] dollars. See our staff for an application" );
    }
  }

同样在这里。

</script>
</head>
<body>
<h2> Applicant Screening Page </h2>
<hr>
<form id = "myForm" action = "">
    <p>Email:<input type = "email" id = "email"></p>
    <p>Age:<input type = "number" maxlength="3" size="3" id = "age"></p>

您可以在年龄输入后放置一个空的跨度来放入消息。

    <p><label>Check the box if you hold a college degree: <input type = "checkbox" id = "degree"></label></p>

表单控件需要名称才能成功,它们通常不需要ID。

    <p>Email:<input type="email" name="email"></p>
    <p>Age:<input type ="number" maxlength="3" size="3" name="age"></p>
    <p><label>Check the box if you hold a college degree: <input type="checkbox" name="degree"></label></p>
    <br>
    <form action="">

您不能在表单中放置表单,只需删除上面的标记(它没有结束标记)。

    <input type="button" value="Determine Salary" onclick="salary()">

您需要提交按钮才能提交表单。

</form>
</body>
</html>

因此,如果您需要更多帮助,请继续更新代码并返回。

相关问题