简体   繁体   English

javascript无法正常执行; 不确定为什么。 验证表格

[英]javascript won't execute properly; unsure why. validating form

My javascript won't properly load. 我的JavaScript无法正确加载。 I am not sure why. 我不知道为什么。 I am new to programming/scripting.. i appreciate the help. 我是编程/脚本开发的新手。

The idea behind this is to validate a form without using alerts, however it does not seem to execute at all. 其背后的想法是在不使用警报的情况下验证表单,但是它似乎根本没有执行。

Here's my code: 这是我的代码:

validateForm()

    {
var province = document.getElementByID("province");
var postalcode = document.getElementByID("postalcode");

    if(province.value == "Select a province")  
    {  
        document.write('Select your province from the list');  
        province.focus();  
        return false;   
    }             
    else  
    {     
        return true;  
    }  

    if(postalcode.length<6)
    {
        document.write("You must enter a valid postal code .");
    }


    var widget1qty=document.getElementById("widget1qty").innerHTML;
    var widget2qty=document.getElementById("widget2qty").innerHTML;
    var widget3qty=document.getElementById("widget3qty").innerHTML;


        if(widget1qty ==0)
        {
    document.write("You must select some widgets.");
        }

        if(widget2qty < 0 )
        {
    document.write("You must select some widgets.");
        }

        if(widget3qty < 0 )
        {
    document.write("You must select some widgets.");
        }

                    }

HTML: HTML:

    <h2>Order Form</h2>
    <form name="myForm" method="post" action="processForm.html" onsubmit="validateForm()">
    <table>
        <tr>
            <th colspan="2">Personal Information</th>
        </tr>
        <tr>
            <td>First Name:</td>
            <td><input type="text" name="firstName" id="firstName" size="30"  required></td>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td><input type="text" name="lastName" id="lastName" size="30" required></td>
        </tr>
        <tr>
            <td>Address:</td>
            <td><input type="text" name="address" id="address" size="30" required></td>
        </tr>
        <tr>
            <td>City:</td>
            <td><input type="text" name="city" id="city" size="30" required></td>
        </tr>
        <tr>
            <td>Province:</td>
            <td><select name="province" id="province" size="1" required>
                    <option disabled>Select a province</option>
                    <option value="BC">British Columbia</option>
                    <option value="AB">Alberta</option>
                    <option value="SK">Saskatchewan</option>
                    <option value="MB">Manitoba</option>
                    <option value="ON">Ontario</option>
                    <option value="QC">Québec</option>
                    <option value="NB">New Brunswick</option>
                    <option value="NS">Nova Scotia</option>
                    <option value="PE">Prince Edward Island</option>
                    <option value="NF">Newfoundland</option>
                    <option value="YK">Yukon</option>
                    <option value="NWT">Northwest Territories</option>
                    <option value="NU">Nunavut</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Postal Code:</td>
            <td><input type="text" name="postalCode" id="postalCode" maxlength="6"  minlength="6" required></td>
        </tr>
        <tr>
            <th colspan="2">Order Information</th>
        </tr>
        <tr>
            <td rowspan="3">Select your products:<br>
                <span id="productError" class="errorMessage" hidden></span></td>
            <td>Widget #1&nbsp;
                <input type="text" name="widget1qty" id="widget1qty" size="1" value="0">Qty @ <strong>$5.00/ea</strong></td>
        </tr>
        <tr>
            <td>Widget #2&nbsp;
                <input type="text" name="widget2qty" id="widget2qty" size="1" value="0">Qty @ <strong>$15.00/ea</strong></td>
        </tr>
        <tr>
            <td>Widget #3&nbsp;
                <input type="text" name="widget3qty" id="widget3qty" size="1" value="0">Qty @ <strong>$25.00/ea</strong></td>
        </tr>
        <tr>
            <td rowspan="3">Shipping Type:</td>
            <td>Standard ($5.00)<input type="radio" name="shippingType" id="shippingTypeStandard" value="Standard" checked></td>

        </tr>
        <tr>
            <td>Express ($10.00)<input type="radio" name="shippingType" id="shippingTypeExpress" value="Express"></td>
        </tr>
        <tr>
            <td>Overnight ($20.00)<input type="radio" name="shippingType" id="shippingTypeOvernight" value="Overnight"></td>
        </tr>
        <tr>
            <th colspan="2">Submit Order</th>
        </tr>
        <tr>
            <td><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit Order" onSubmit="validateForm()"></td>
            <td><input type="reset" name="btnReset" id="btnReset" value="Reset Form" ></td>
        </tr>
    </table>
    </form>
</body>

` `

Is that your full javascript? 那是您的完整JavaScript吗? It is going to be pulling an unexpected end since you haven't finished your closures. 由于您还没有完成关闭操作,这将是意外的结束。 If you are using Google Chrome to view the HTML, hit F12 to get the developer tools and view the console. 如果您使用的是Google Chrome浏览器来查看HTML,请按F12键以获取开发人员工具并查看控制台。

Also it is getElementById or getElementsByName 也是getElementById或getElementsByName

document.getElementById(string);

That call can only return one element since Ids are unique. 由于Id是唯一的,因此该调用只能返回一个元素。

document.getElementsByName(string);

That call can return multiple elements as names are not unique. 该调用可以返回多个元素,因为名称不是唯一的。 Meaning you are going to get an array even if there is one. 这意味着即使有一个数组,您也将获得一个数组。


  1. You don't need the excess 'else's. 您不需要多余的“ else”。 If you don't have logic in it, no need to include it. 如果其中没有逻辑,则无需包含它。
  2. You are getting the same widget value and handing it into widgetqty1, widgetqty2, and widgetqty3. 您将获得相同的窗口小部件值,并将其传递给widgetqty1,widgetqty2和widgetqty3。
  3. You never close the function definition. 您永远不会关闭函数定义。 You need the last ending curly brace (}) to make sure it is a valid function. 您需要最后一个大括号(})来确保它是有效的函数。

You should end up with the following code: 您应该以以下代码结束:

<html>
<head>
    <script>
        function validateForm(ev){
            var province, postalcode, widget1qty, widget2qty, widget3qty;

            province = document.getElementById("province");
            postalcode = document.getElementById("postalcode");
            widget1qty = document.getElementById("widget1qty").innerHTML;
            widget2qty = document.getElementById("widget2qty").innerHTML;
            widget3qty = document.getElementById("widget3qty").innerHTML;

            if(province.value == "Select a province")  
            {  
                document.write('Select your province from the list');  
                // You realize this overwrites your entire document?  
                province.focus();  
                return false;   
            }

            if(postalcode.length<6)
            {
                document.write("You must enter a valid postal code .");
                // You realize this overwrites your entire document?  
                postcalcode.focus();
                return false;
            }

            if(widget1qty == 0 || widget2qty < 0 || widget3qty < 0)
            {
                document.write("You must select some widgets.");
                // You realize this overwrites your entire document?  
                return false;
            }
        }
    </script>
</head>
<body>
    <h2>Order Form</h2>
    <form onsubmit="return validateForm()"name="myForm" method="post" action="processForm.html">
    <table>
        <tr>
            <th colspan="2">Personal Information</th>
        </tr>
        <tr>
            <td>First Name:</td>
            <td><input type="text" name="firstName" id="firstName" size="30"  required></td>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td><input type="text" name="lastName" id="lastName" size="30" required></td>
        </tr>
        <tr>
            <td>Address:</td>
            <td><input type="text" name="address" id="address" size="30" required></td>
        </tr>
        <tr>
            <td>City:</td>
            <td><input type="text" name="city" id="city" size="30" required></td>
        </tr>
        <tr>
            <td>Province:</td>
            <td><select name="province" id="province" size="1" required>
                    <option disabled>Select a province</option>
                    <option value="BC">British Columbia</option>
                    <option value="AB">Alberta</option>
                    <option value="SK">Saskatchewan</option>
                    <option value="MB">Manitoba</option>
                    <option value="ON">Ontario</option>
                    <option value="QC">Québec</option>
                    <option value="NB">New Brunswick</option>
                    <option value="NS">Nova Scotia</option>
                    <option value="PE">Prince Edward Island</option>
                    <option value="NF">Newfoundland</option>
                    <option value="YK">Yukon</option>
                    <option value="NWT">Northwest Territories</option>
                    <option value="NU">Nunavut</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Postal Code:</td>
            <td><input type="text" name="postalCode" id="postalCode" maxlength="6"  minlength="6" required></td>
        </tr>
        <tr>
            <th colspan="2">Order Information</th>
        </tr>
        <tr>
            <td rowspan="3">Select your products:<br>
                <span id="productError" class="errorMessage" hidden></span></td>
            <td>Widget #1&nbsp;
                <input type="text" name="widget1qty" id="widget1qty" size="1" value="0">Qty @ <strong>$5.00/ea</strong></td>
        </tr>
        <tr>
            <td>Widget #2&nbsp;
                <input type="text" name="widget2qty" id="widget2qty" size="1" value="0">Qty @ <strong>$15.00/ea</strong></td>
        </tr>
        <tr>
            <td>Widget #3&nbsp;
                <input type="text" name="widget3qty" id="widget3qty" size="1" value="0">Qty @ <strong>$25.00/ea</strong></td>
        </tr>
        <tr>
            <td rowspan="3">Shipping Type:</td>
            <td>Standard ($5.00)<input type="radio" name="shippingType" id="shippingTypeStandard" value="Standard" checked></td>

        </tr>
        <tr>
            <td>Express ($10.00)<input type="radio" name="shippingType" id="shippingTypeExpress" value="Express"></td>
        </tr>
        <tr>
            <td>Overnight ($20.00)<input type="radio" name="shippingType" id="shippingTypeOvernight" value="Overnight"></td>
        </tr>
        <tr>
            <th colspan="2">Submit Order</th>
        </tr>
        <tr>
            <td><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit Order" ></td>
            <td><input type="reset" name="btnReset" id="btnReset" value="Reset Form" ></td>
        </tr>
    </table>
    </form>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM