表格验证?

时间:2016-04-19 22:23:15

标签: javascript html forms validation

我只想尝试进行此检查表单验证。就像它不应该让表单提交,除非所有内容都填写完毕,那么只有在它完成时才应该完成。我是新手,我不知道发生了什么,但默认情况下它只是在我的屏幕顶部显示一个蓝色框,然后提交/接受,无论表格是否填写。 HTML:

#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
vector<float> vec{0.1,0.9,0.2,0.8,0.3,0.7,0.4,0.6,0.5,1};

Mat m1( vec ); 
imshow("m1",m1);
waitKey();

Mat m2( 1,vec.size(), CV_32FC1,vec.data());
imshow("m2",m2);
waitKey();

Mat1f m3( vec.size(), 1, vec.data());
imshow("m3",m3);
waitKey();

Mat1f m4( 1, vec.size(), vec.data());
imshow("m4",m4);
waitKey();

cout << "as seen below all Mat and vector use same data" << endl;
cout << vec[0] << endl;
m1 *= 2;
cout << vec[0] << endl;
m2 *= 2;
cout << vec[0] << endl;
m3 *= 2;
cout << vec[0] << endl;
m4 *= 2;
cout << vec[0] << endl;

return 0;
}

JavaScript的:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hands-on Project 6 - Order</title>
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>

<body>

<section>


    <article>
        <h2>Pizza Order Form</h2>
        <div id="errorMessage">
        </div>
        <form action="results.html" id="orderForm">
            <input type="hidden" name="hidden" id="hidden">
            <fieldset>


                <legend>Delivery Information</legend>

                <label for="nameinput">Name</label>
                <input type="text" id="nameinput" name="name">

                <label for="addrinput">Street Address</label>
                <input type="text" id="addrinput" name="address">

                <label for="cityinput">City</label>
                <input type="text" id="cityinput" name="city">

                <label for="emailinput">Email</label>
                <input type="email" id="emailinput" name="email">

                <label for="phoneinput">Phone</label>
                <input type="tel" id="phoneinput" name="phone">

            </fieldset>

            <fieldset>

                <legend>Order</legend>

                <p>
                    <span class="nonLabel">What type of crust:</span>

                    <br>

                    <input type="radio" name="crust" id="thin" value="1">
                    <label for="thin">Thin Crust</label>

            <input type="radio" name="crust" id="original" value="0">
                    <label for="original">Original Crust</label>

                    <input type="radio" name="crust" id="thick" value="3">
                    <label for="thick">Deep Dish</label>
                </p>

                <label for="size" class="nonLabel">What size pizza:</label>

                <select id="size" name="size">
                    <option value=""> &nbsp; </option>
                    <option value="small">Small</option>
                    <option value="medium">Medium</option>
                    <option value="large">Large</option>
                    <option value="XL">Extra Large</option>
                </select>

                <p>
                    <span class="nonLabel">What topping(s):</span>

                    <br>

    <input type="checkbox" id="pepperoni" name="toppings" value="Pepperoni">
                    <label for="pepperoni">Pepperoni</label>

        <input type="checkbox" id="sausage" name="toppings" value="Sausage">
                    <label for="sausage">Sausage</label>

            <input type="checkbox" id="bacon" name="toppings" value="Bacon">
                    <label for="bacon">Bacon</label>

                    <br>

<input type="checkbox" id="greenpep" name="toppings" value="Green Peppers">
                    <label for="greenpep">Green Peppers</label>

        <input type="checkbox" id="onion" name="toppings" value="Onions">
                    <label for="onion">Onions</label>

 <input type="checkbox" id="xcheese" name="toppings" value="Extra Cheese">
                    <label for="xcheese">Extra Cheese</label>
                </p>

                <p>
    <label for="instructions" id="instrlabel">Special Instructions:</label>
                </p>

<textarea id="instructions" name="instructions" rows="3" cols="60" 

 placeholder="Special Requests, Delivery Details"></textarea>

            </fieldset>

            <fieldset id="submitbutton">

                <input type="submit" id="submitBtn" value="Add to Cart">

            </fieldset>

        </form>

    </article>

    </section>
    <script>
    document.getElementById("submitBtn ").addEventListener("submit",   

 validateForm(evt));
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

错误的id参数

&#13;
&#13;
document.getElementById("submitButton ").addEventListener("submit", validateForm(evt));
&#13;
&#13;
&#13;

必须

&#13;
&#13;
document.getElementById("submitBtn").addEventListener("submit",validateForm(evt));
&#13;
&#13;
&#13;