Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.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_Jquery_Html_Validation_Input - Fatal编程技术网

javascript未验证表单中的输入

javascript未验证表单中的输入,javascript,jquery,html,validation,input,Javascript,Jquery,Html,Validation,Input,大家好,我有一个表单,可以将用户输入的内容发布到同一个页面的表中,而无需重新加载。我已经完成了将输入传递到表的简单部分,并开始进行验证,但由于某种原因,它似乎不起作用。请查看下面我的代码 先谢谢你 <script type="text/javascript"> var qtyTotal = 0; var priceTotal = 0; var totalCart = 0; function updateForm() { fail =

大家好,我有一个表单,可以将用户输入的内容发布到同一个页面的表中,而无需重新加载。我已经完成了将输入传递到表的简单部分,并开始进行验证,但由于某种原因,它似乎不起作用。请查看下面我的代码

先谢谢你

<script type="text/javascript">
    var qtyTotal = 0;
    var priceTotal = 0;
    var totalCart = 0;

    function updateForm() {
        fail  = validateProduct(form.product.value)
        fail += validateQuantity(form.quantity.value)
        fail += validatePrice(form.price.value)
        if (fail == ""){
        var product = document.getElementById("product").value;

        var qty = document.getElementById("quantity").value;
        qtyTotal = qtyTotal + parseFloat(qty);


        var price = document.getElementById("price").value;    
        priceTotal = (qty * parseFloat(price)).toFixed(2);

        totalCart = totalCart + parseFloat(priceTotal);
        document.getElementById("totalCart").innerHTML="$"+totalCart;

        var table=document.getElementById("results");
        var row=table.insertRow(-1);
        var cell1=row.insertCell(0);
        var cell2=row.insertCell(1);
        var cell3=row.insertCell(2);
        var cell4=row.insertCell(3);
        cell1.innerHTML=product;
        cell2.innerHTML=qty;        
        cell3.innerHTML="$"+price;
        cell4.innerHTML="$"+priceTotal;
        }
        else{ 
   document.getElementById('errors').innerHTML = fail;}
   return false
    }


        function validateProduct(field) {
            if (field == "") return "No product name was entered.<br/>"
            else if (field.length < 3) return "Please enter a valid name.<br/>"
            return ""
            }
        function validateQuantity(field) {
            if (field == "") return "No quantity was entered.<br/>"
            else if (!/[0-9]*$/.test(field)) return "Quantity can only have numerical values.<br/>"
            return ""
            }
        function validatePrice(field) {
            if (field == "") return "No city was entered.<br/>"
            else if (field.length < 2) return "Please enter a valid price with two decimal points.<br/>"
            else if (!/[0-9]+(\.[0-9][0-9]?)?/.test(field)) return "Price can only have numerical values with two decimal points.<br/>"
            return ""
            }

</script>

<h1>Instructions</h1>
<section>
    <p>Please enter the desired product/services in the following table to create an order.</p>
<section style="width:300px; margin-left:20px">
<div id="errors"></div>
        <form name="order" id="order">
    <table>
        <tr>
            <td>
                <label for="product">Product:</label>
            </td>
            <td>
                <input id="product" name="product" title="Please enter only alphabetic characters" type="text" size="28" />
            </td>
        </tr>
        <tr>
            <td>
                <label for="quantity">Quantity:</label>
            </td>
            <td>
                <input id="quantity" name="quantity" title="Enter item quantity" width="196px" />
            </td>
        </tr>
        <tr>
            <td>
                <label for="price">Price:</label>
            </td>
            <td>
                <input id="price" name="price" title="Please enter only numeric characters" size="28" />
            </td>
        </tr>
    </table>
    <input type="reset" name="reset" value="Reset" />
    <input type="button" onClick="updateForm();" value="Add to Cart"/>
</form>
    </section>
</section>
<section>
<br>
<p id="header">INVOICE</p>
<table id="results" width="599">
    <thead>
    <tr>
        <th scope="col" width="96">Products</th>
        <th scope="col" width="94">Quantity</th>
        <th scope="col" width="98">Unit Cost</th>
        <th scope="col" width="52">Price</th>
    </tr>
    </thead>
</table>

<table id="resultTotals" width="599">
<tr>
    <td colspan="2"></td>
    <td style="padding-left:5px" bgcolor="#CCCCCC" scope="col" width="161">Total</td>
    <td style="padding-left:5px" bgcolor="#CCCCCC" scope="col" width="91"><div id="totalCart"></div></td>
</tr>
</table>
</section>

var-qtyTotal=0;
var-priceTotal=0;
var totalCart=0;
函数updateForm(){
fail=validateProduct(form.product.value)
fail+=validateQuantity(form.quantity.value)
fail+=validatePrice(form.price.value)
如果(失败==“”){
var product=document.getElementById(“产品”).value;
变量数量=document.getElementById(“数量”).value;
qtyTotal=qtyTotal+parseFloat(数量);
var price=document.getElementById(“价格”).value;
价格总计=(数量*浮动(价格)).toFixed(2);
totalCart=totalCart+parseFloat(价格合计);
document.getElementById(“totalCart”).innerHTML=“$”+totalCart;
var table=document.getElementById(“结果”);
var行=table.insertRow(-1);
var cell1=行插入单元格(0);
var cell2=行插入单元格(1);
var cell3=行插入单元格(2);
var cell4=行插入单元格(3);
cell1.innerHTML=产品;
cell2.innerHTML=数量;
cell3.innerHTML=“$”+价格;
cell4.innerHTML=“$”+价格总计;
}
否则{
document.getElementById('errors')。innerHTML=fail;}
返回错误
}
功能验证产品(字段){
如果(字段==“”)返回“未输入产品名称。
” 否则,如果(field.length<3)返回“请输入有效名称。
” 返回“” } 函数验证相等性(字段){ 如果(字段==“”)返回“未输入数量。
” 否则如果(!/[0-9]*$/.test(field))返回“数量只能有数值。
” 返回“” } 功能验证价格(字段){ 如果(字段=“”)返回“未输入任何城市。
否则,如果(field.length<2)返回“请输入带两个小数点的有效价格。
” 否则如果(!/[0-9]+(\[0-9][0-9]?)?/.test(field))返回“价格只能有带两个小数点的数值。
” 返回“” } 说明书 请在下表中输入所需的产品/服务以创建订单

产品: 数量: 价格:
发票

产品 量 单位成本 价格 全部的
您没有关闭函数
updateForm
(或者更具体地说,您没有关闭
if(fail==“”)上的
else
,这将使用函数close来代替。

我可以看到另一个问题:您没有定义
表单
变量。请尝试以下操作:

function updateForm() {
    var form = document.getElementById('order'); // added
    fail = validateProduct(form.product.value);
    ...

所以我基本上必须把我的else放在if支架内?