验证表单编号类型Javascript

时间:2013-09-19 09:25:07

标签: javascript html validation

嗨,我有这个问题。在我的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>

3 个答案:

答案 0 :(得分:1)

更改

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

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

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

[编辑]

请参阅更新的小提琴:http://jsfiddle.net/5UxGp/1/

答案 1 :(得分: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;
      }
    }

答案 2 :(得分:0)

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

function isNumber(x) {
    return !isNaN(parseFloat(x)) && isFinite(x);
}