简体   繁体   English

使用 Javascript 验证表单

[英]Validating a form using Javascript

I'm having problems trying to validate an entire form using Javascript.我在尝试使用 Javascript 验证整个表单时遇到问题。

"Add JavaScript code to produce an error message and suppress submission of the form if any quantity field contains non-numeric data. (It's OK for a quantity to be empty, but if it's non-empty, it must have only numbers.) Add an action= attribute to your tag to submit the form to a website (not going to put actual website). Test that the form is submitted correctly when the quantities are numeric or empty, and that an error message is produced otherwise." “添加 JavaScript 代码以生成错误消息并在任何数量字段包含非数字数据时禁止提交表单。(数量可以为空,但如果数量不为空,则它必须只有数字。)添加“您的标签的 action= 属性用于将表单提交到网站(不打算放置实际网站)。测试当数量为数字或空时表单是否正确提交,否则会产生错误消息。”

I've done all the instructions asked me to do and it isn't working.我已经完成了所有要求我做的说明,但它不起作用。 We are supposed to use the form that we created in one of the previous labs.我们应该使用我们在之前的实验室之一中创建的表单。 Here is the code that I have been working on.这是我一直在研究的代码。

 <!DOCTYPE html>
<html>
    <head>
        <title>Lab 6, Part 3</title>
        <meta charset="UTF-8"/>
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
        <script type="text/javascript">
        function validateForm(){
            var a = document.forms["myform"]["Pokemon"].value;
            var b = document.forms["myform"]["Pokeball"].value;
            var c = document.forms["myform"]["Pikachu"].value;
            var d = document.forms["myform"]["firstname"].value;
            var e = document.forms["myform"]["lastname"].value;
            var f = document.forms["myform"]["streetaddress"].value;
            var g = document.forms["myform"]["city"].value;
            var h = document.forms["myform"]["zipcode"].value;

            if (a = null || a == ""){
                alert("Grapes must be filled out!");
                return false;
            }

            if (b = null || b == ""){
                alert("Cherries must be filled out!");
                return false;
            }

            if (c = null || c == ""){
                alert("Strawberries must be filled out!");
                return false;
            }

            if (d = null || d == ""){
                alert("First Name must be filled out!");
                return false;
            }

            if (e = null || e == ""){
                alert("Last Name must be filled out!");
                return false;
            }

            if (f = null || f == ""){
                alert("Street Address must be filled out!");
                return false;
            }

            if (g = null || g == ""){
                alert("City must be filled out!");
                return false;
            }

            if (h = null || h == ""){
                alert("Zip Code must be filled out!");
                return false;
            }


        }
        </script>
    </head>
    <body><form>
        <form name="myform" action="http://weblab.kennesaw.edu/formtest.php" 
    onsubmit="return validateForm()" 
              method = "post"> 
        <h1 style="text-align:center">Lab 6, Part 3</h1>
        <h2 style="text-align:center">IT 3203</h2>
        <a href="index.html"><p style="text-align:center">Main Page!</p></a>
            <p>Grapes</p><input type=text size=3 maxlength=3 name="Pokemon">
            <p>Cherries</p><input type=text size=3 maxlength=3 name="Pokeball">
            <p>Strawberries</p><input type=text size=3 maxlength=3 name="Pikachu">
        <br>
        <label>First Name
            <input type="text"
                   name="firstname" id="firstname"
                   size="25" />
        </label>
        <br>
        <br>
        <label>Last Name
            <input type="text"
                   name="lastname" id="lastname"
                   size="25" />
        </label>
        <br>
        <br>
        <label>Street Address
            <input type="text"
                   name="streetaddress" id="streetaddress"
                   size="35" />
        </label>
        <br>
        <br>
        <label>City
            <input type="text"
                   name="city" id="city"
                   size="25" />
        </label>
        <label>State:
            <select name="state">
                <option>Please Select</option>
                <option>Alabama</option>
                <option>Alaska</option>
                <option>Arizona</option>
                <option>Arkansas</option>
                <option>California</option>
                <option>Colorado</option>
                <option>Connecticut</option>
                <option>Delaware</option>
                <option>Florida</option>
                <option>Georgia</option>
                <option>Hawaii</option>
                <option>Idaho</option>
                <option>Illinois</option>
                <option>Indiana</option>
                <option>Iowa</option>
                <option>Kansas</option>
                <option>Kentucky</option>
                <option>Louisiana</option>
                <option>Maine</option>
                <option>Maryland</option>
                <option>Massachusetts</option>
                <option>Michigan</option>
                <option>Minnesota</option>
                <option>Mississippi</option>
                <option>Missouri</option>
                <option>Montana</option>
                <option>Nebraska</option>
                <option>Nevada</option>
                <option>New Hampshire</option>
                <option>New Jersey</option>
                <option>New Mexico</option>
                <option>New York</option>
                <option>North Carolina</option>
                <option>North Dakota</option>
                <option>Ohio</option>
                <option>Oklahoma</option>
                <option>Oregon</option>
                <option>Pennsylvania</option>
                <option>Rhode Island</option>
                <option>South Carolina</option>
                <option>South Dakota</option>
                <option>Tennessee</option>
                <option>Texas</option>
                <option>Utah</option>
                <option>Vermont</option>
                <option>Virginia</option>
                <option>Washington</option>
                <option>West Virginia</option>
                <option>Wisconsin</option>
                <option>Wyoming</option>
            </select>
        </label>
        <br>
        <br>
        <label>Zip code:
            <input type="text"
                   name="zipcode" id="zipcode"
                   size="20" />

        </label>
        </form>
        <br>
        <br>
        <label>Visa
            <input type="radio" name="pref_payment"
                   id="pref_payment_visa" value="visa" checked />
        </label><br />
        <label>MasterCard
            <input type="radio" name="pref_payment"
                   id="pref_payment_master" value="master" checked />
        </label><br />
        <label>American Express
            <input type="radio" name="pref_payment"
                   id="pref_payment_american" value="american" checked />
        </label><br />
        <input type="button" onclick="confirmation()" value="submit">
        </form>
    </body>
</html>

It is probably something minor that I have overlooked or something.这可能是我忽略的小事或其他什么。 Oh and we had to save the files in .php so maybe that has a lot to do with it.哦,我们不得不将文件保存在 .php 中,所以也许这与它有很大关系。

1) Check your form tag. 1) 检查您的表单标签。 You got two there.你那里有两个。

<form>

2) Your submit button type. 2) 您的提交按钮类型。

Should be:应该:

type="submit"

Not:不是:

type="button"

And no need to add:并且无需添加:

onclick="confirmation()"

http://plnkr.co/edit/Ml2KWgfU5KG5gmBpf8Fe?p=preview http://plnkr.co/edit/Ml2KWgfU5KG5gmBpf8Fe?p=preview

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

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