2015-11-11 01:30
浏览 33


I'm having problems trying to validate an entire form using 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."

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>
        <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;

        <form name="myform" action="" 
    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">
        <label>First Name
            <input type="text"
                   name="firstname" id="firstname"
                   size="25" />
        <label>Last Name
            <input type="text"
                   name="lastname" id="lastname"
                   size="25" />
        <label>Street Address
            <input type="text"
                   name="streetaddress" id="streetaddress"
                   size="35" />
            <input type="text"
                   name="city" id="city"
                   size="25" />
            <select name="state">
                <option>Please Select</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>Rhode Island</option>
                <option>South Carolina</option>
                <option>South Dakota</option>
                <option>West Virginia</option>
        <label>Zip code:
            <input type="text"
                   name="zipcode" id="zipcode"
                   size="20" />

            <input type="radio" name="pref_payment"
                   id="pref_payment_visa" value="visa" checked />
        </label><br />
            <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">

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxin2011 2015-11-11 01:46

    1) Check your form tag. You got two there.


    2) Your submit button type.

    Should be:




    And no need to add:


    点赞 打赏 评论

相关推荐 更多相似问题