Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查表单中的数值或空字符串_Javascript_Html_Regex_Forms - Fatal编程技术网

Javascript 检查表单中的数值或空字符串

Javascript 检查表单中的数值或空字符串,javascript,html,regex,forms,Javascript,Html,Regex,Forms,我使用javascript检查HTML表单中的数值和空字符串。如果输入了任何其他内容(符号、字母等),则不应提交表单,并应弹出错误消息。到目前为止,无论发生什么情况,表单都会提交。我正在尝试设置它,这样如果3个字段中的任何一个有错误的值,它就不会提交。例如:先输入“5”再输入“a”再输入“5”(或类似条目的任意组合)不起作用。而“5”则“5”或“5”则“5”或“5”则“5”。。。。etc应该 JAVASCRIPT函数 function validateOrder()

我使用javascript检查HTML表单中的数值和空字符串。如果输入了任何其他内容(符号、字母等),则不应提交表单,并应弹出错误消息。到目前为止,无论发生什么情况,表单都会提交。我正在尝试设置它,这样如果3个字段中的任何一个有错误的值,它就不会提交。例如:先输入“5”再输入“a”再输入“5”(或类似条目的任意组合)不起作用。而“5”则“5”或“5”则“5”或“5”则“5”。。。。etc应该

JAVASCRIPT函数

  function validateOrder()
                {
                    var myOrderRegex = /^(\s*|\d+)$/

                if(myOrderRegex.test(document.getElementById("appleorderquantity").value))
                {
                    return true;
                }
                else
                {
                    alert("Apple quantity invalid: must be numeric");
                    return false;
                }
                if(myOrderRegex.test(document.getElementById("grapeorderquantity").value))
                {
                    return true;
                }
                else
                {
                    alert("Apple quantity invalid: must be numeric");
                    return false;
                }
                if(myOrderRegex.test(document.getElementById("strbryorderquantity").value))
                {
                    return true;
                }
                else
                {
                    alert("Apple quantity invalid: must be numeric");
                    return false;
                }
            }
<input type="submit" value="Submit" onClick="return validateOrder()" />
用于输入文本框的HTML

<input type="text" name="Apple_Quantity" id="appleorderquantity" size="25" />
<input type="text" name="Grape_Quantity" id="grapeorderquantity" size="25" />
<input type="text" name="Strawberry_Quantity" id="strbryorderquantity" size="25" />

HTML用于提交按钮

  function validateOrder()
                {
                    var myOrderRegex = /^(\s*|\d+)$/

                if(myOrderRegex.test(document.getElementById("appleorderquantity").value))
                {
                    return true;
                }
                else
                {
                    alert("Apple quantity invalid: must be numeric");
                    return false;
                }
                if(myOrderRegex.test(document.getElementById("grapeorderquantity").value))
                {
                    return true;
                }
                else
                {
                    alert("Apple quantity invalid: must be numeric");
                    return false;
                }
                if(myOrderRegex.test(document.getElementById("strbryorderquantity").value))
                {
                    return true;
                }
                else
                {
                    alert("Apple quantity invalid: must be numeric");
                    return false;
                }
            }
<input type="submit" value="Submit" onClick="return validateOrder()" />

在函数结束前不要返回true-检查不应提交的任何条件,如果满足其中任何条件,则返回false,否则,在检查所有条件后,返回true

而且,你最好还是做点什么

<form onsubmit="return validateOrder()">
     ...
</form>