0

你好我有这个问题。在我的 html 代码中,我似乎无法验证我的输入类型“数字”。我使用此代码尝试验证它,但它不起作用

function validateForm()
{
var x=document.forms["form_name"]["number_name"].value;
if (x==null || x=="")
  {
  alert("The following must be filled out");
  return false;
  }
}

我从验证输入类型“文本”而不​​是数字的网站获取此代码。我正在尝试对我的完整 html 代码实施有效的“数字”验证。顺便说一句,这是我的表格示例:

<form action = "test.php" method = "post" onsubmit = "return validateForm()" name ="form_name">
<input type = "number" min = "0" placeholder = "0" name = "number_name" size = "2"/>

我想知道是否可以使用上面的 javascript valdiation 来验证数字形式,或者是否有更简单的方法来做到这一点。

*深入 * 我为我的第一个问题制作了一个快速的 html 代码,并制作了多种形式的“数字”。它不完整......我决定在实现整个表单的代码之前测试一个“数字” 这是我的代码到目前为止:

<html>
    <head>
    <script language="javascript">
    function validateForm()
    {
    var x=document.forms["order"]["cappuccino_qty"].value;
    if (x >= 0 || x < 0);
    {
    alert("The following must be filled out");
    return false;
    }
    }
    </script>   
    <body>              
    <form action = "test.php" method = "post" onsubmit = "return validateForm()" name ="order">
                <label class = "field" for = "Cappucino">Cappuccino
                <input type = "number" min = "0" placeholder = "$3.75" name = "cappuccino_qty" size = "2"/><br>
                <label class = "field" for = "Espresso">Espresso
                <input type = "number" min = "0" placeholder = "$3.00" name = "espresso_qty" size = "2"/><br>
                <label class = "field" for = "Double Espresso">Double Espresso
                <input type = "number" min = "0" placeholder = "$4.25" name = "double_espresso_qty" size = "2"/><br>
                <label class = "field" for = "Flat White">Flat White
                <input type = "number" min = "0" placeholder = "$3.75" name = "flat_white_qty" size = "2"/><br>
                <label class = "field" for = "Latte">Latte  
                <input type = "number" min = "0" placeholder = "$3.50" name = "latte_qty" size = "2"/><br>
                <label class = "field" for = "Ice Coffee">Ice Coffee
                <input type = "number" min = "0" placeholder = "$2.50" name = "ice_qty" size = "2"/><br>
                <input type = "submit" value = "submit" name = "submit"/>
                <p>
       </form>
       </body>  
       </head>
       </Html>
4

3 回答 3

1

改变

if (x==null || x=="")

if (/[^\d\.]/.test(x))

这会检查任何非数字或句点字符(假设您要允许小数)。

[编辑]

查看更新的小提琴:http: //jsfiddle.net/5UxGp/1/

于 2013-09-19T09:32:08.873 回答
1

一个简单的方法

function validateForm()
{
var x=document.forms["form_name"]["number_name"].value;
if (!(x >= 0) || !(x < 0))
  {
  alert("The following must be filled out");
  return false;
  }
}

顺便说一句,如果 0 在您的表单中是最小值

 function validateForm()
    {
    var x=document.forms["form_name"]["number_name"].value;
    if (!(x >= 0))
      {
      alert("The following must be filled out");
      return false;
      }
    }
于 2013-09-19T09:36:30.517 回答
0

您只测试变量是否为空或空,但是当您在其上输入字符时,它会跳出您的 if 语句。尝试使用此功能。如果需要,请参阅此处的参考:Validate decimal numbers in JavaScript - IsNumeric()。希望有帮助。

function isNumber(x) {
    return !isNaN(parseFloat(x)) && isFinite(x);
}
于 2013-09-19T09:54:31.917 回答