JavaScript 无法验证 HTML 中的表单

标签 javascript html css

您好,我在尝试验证简单表单时遇到问题。我不是最擅长 Javascript,并且不确定我哪里出错了。这可能是一个简单的拼写错误,也可能是我看不到的代码之间的完全冲突。感谢所有帮助。抱歉,代码量很大,我想确保您看到每个细节。

<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet"
   type = "text/css"
   href = "Stylesheet.css" />
<title>Customer details form</title>
<script src="gen_validatorv4.js" type="text/javascript"></script>

<!-- ============================== My failed attempt to try and validate my form with Javascript ============================== -->

<script>
function check() {
    document.getElementById("Agree").checked = true;
}
function uncheck() {
    document.getElementById("Agree").checked = false;
}
</script>
<center><img src= Images/HorizonHomePageTitle.png ALT="Horizon Bed and Breakfast Title"></center>
</head>
<body>

<script>
window.alert(Please enter data where * is assigned.);
</script>

<center><a href="XML/BudgetRooms.xml" target="_blank">Budget Rooms</a><center>
 <HR COLOR="blue" WIDTH="60%">
  <center><a href="XML/LuxuryRooms.xml" target="_blank">Luxury Rooms</a><center>
 <HR COLOR="blue" WIDTH="60%">
  <center><a href="Index.html" target="_blank">Home</a><center>
 <HR COLOR="blue" WIDTH="60%">
  <center><a href="https://www.visser.com.au/blog/generic-privacy-policy-for-australian-websites/" target="_blank">Our privacy policy</a><center>
 <HR COLOR="blue" WIDTH="60%">
  <center><a href="https://media.termsfeed.com/pdf/terms-and-conditions-template.pdf" target="_blank">Our terms of conditions</a><center>
 <HR COLOR="blue" WIDTH="60%">
  <center><a href=" http://www.blogtyrant.com/best-about-us-pages/" target="_blank">Our terms and conditions</a><center>
 <HR COLOR="blue" WIDTH="60%">
<center><a href="http://www.blogtyrant.com/best-contact-us-pages/" target="_blank">Contact us</a><center>

<form id='CustomerDetailForm' action="">

        <!-- ============================== Fieldset 1 - includes personal information of clients ============================== -->
        <fieldset>
            <legend>Personal Information:</legend>
            <hr class="form" />
                <label for="Full Name"><strong>Full Name:*</strong></label>
                <input name="FullNm" type="text" size="20" id="FN" class="text"/>

                <label for="Email"><strong>Email:</strong></label><br/>
                <input name="Emil" type="text" size="20" id="EM" class="text"/>

                                <label for="Age"><strong>Input age if between 18 and 100:</strong></label>
                <input name="Ages" type="text" size="20" id="AG" class="text"/> 

                                <label for="select" class="choose"><strong>On what continent do you live?</strong></label>
                    <select id="select" name="select">
                        <option value="1">Africa</option>
                        <option value="2">Europe</option>
                        <option value="3">Asia</option>
                        <option value="4">North Amrica</option>
                                                <option value="5">South America</option>
                                                <option value="6">Ausralia</option>
                                        </select>
        </fieldset>
        <!-- ============================== Fieldset 2 - Includes further optional information from clients to help productivity ============================== -->
        <fieldset>
            <legend>Questions:</legend>
            <hr class="form" />
                <label for="select2" class="choose">How long was your stay?</label><br />
                    <select id="select" name="select2">
                        <option value="7">Less than one week</option>
                        <option value="8">More than one week</option>
                        <option value="9">Months/years</option>
                    </select>

                <label for="select3" class="choose">How many stars would you rate our BnB?</label><br />
                    <select id="selectCon" name="select3">
                        <option value="10">One star</option>
                        <option value="11">Two star</option>
                        <option value="12">Three star</option>
                        <option value="13">Four star</option>
                                                <option value="14">Five star</option>
                                        </select>
                </fieldset>
        <!-- ============================== Fieldset 3 - Holds the comments section, aggreement radio button, submit button and a date button for the client  ============================== -->
        <fieldset>
            <legend>Further commetns:</legend>
            <hr class="form" />
            <textarea name="message" id="message" cols="20" rows="10"></textarea><br />
            <label for="protection" class="spam-protection">Spam check: 1 + 1=</label><br />
            <input name="ochrana" type="text" id="protection" class="answer" /><br />

    Do you agree to our terms?*<br>
    <input type="radio" name="Agreement" id="Agree">Agree<br>

    <button onclick="formValidation()">SUBMIT</button>

<script>
function submitFunction() {
    alert("Thankyou for your submission");
}
</script>

<script>
    function formValidation()  
    {  
    var uname = document.registration.FullNm;  
    var uemail = document.registration.Emil;  
    var uage = document.registration.Ages;  
    var ucon = document.registration.select;  
    {  
    if(nameinput(FullName,2,25))  
    {  
    if(emailval(uemail)) 
    {  
    if(agenum(ag))  
    {   
    if(conselect(con))  
    {  
    }  
    }   
    }  
    }   
    }  
    }  
    }  
    }  
    return false;  
    }  

    function nameinput(uname,2,25)  
    {  
    var uname_len = uname.value.length;  
    if (uname_len == 0 || uname_len >= 2 || uname_len < 25)  
    {  
    alert("Full Name is needed. Between "+2+" to "+25" characters.);  
    uname.focus();  
    return false;  
    }  
    return true;  
    }  

function emailval(uemail,2,25)  
    {  
    var uemail_len = uemail.value.length;  
    if (uemail_len == 0 || uemail_len >= 2 || uemail_len < 30)  
    {  
    alert("Email is required.");  
    uemail.focus();  
    return false;  
    }  
    return true;  
    }  

function agenum(uage,18,100)  
    {  
    var uage_len = uage.value.length;  
    if (uage_len == 0 || uage_len >= 18 || uage_len < 100)  
    {  
    alert("Age is required. Between 18 and 100.");  
    uage.focus();  
    return false;  
    }  
    return true;  
    }  

function conselect(ucon)  
    {  
    if(ucon.value == "Default")  
    {  
    alert('Select your continent from the list');  
    ucon.focus();  
    return false;  
    }  
    else  
    {  
    return true;  
    }  
    }   
</script>
</form>

<button type="button"
onclick="document.getElementById('date/time').innerHTML = Date()">
Display date and time.</button>
<p id="date/time"></p>

               </fieldset>   
</form>
</body>
</html>

最佳答案

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="Stylesheet.css" />
    <title>Customer details form</title>
    <script src="gen_validatorv4.js" type="text/javascript"></script>
    <!-- ============================== My failed attempt to try and validate my form with Javascript ============================== -->
    <script type="text/javascript">
        function check() {
            document.getElementById("Agree").checked = true;
        }
        function uncheck() {
            document.getElementById("Agree").checked = false;
        }

        function formValidation() {
            var uname = document.getElementById("FullNm");
            var uemail = document.getElementById("Email");
            var uage = document.getElementById("Ages");
            var ucon = document.getElementById("select1");

            if (nameInput(uname)) {
                if (emailVal(uemail)) {
                    if (ageNum(uage)) {
                        if (conSelect(ucon)) {
                        }
                    }
                }
            }
        }

        function nameInput(uname) {
            var uname_len = uname.value.length;
            if (uname_len == 0 || uname_len <= 2 || uname_len > 25) {
                alert("Full Name is needed. Between 2 to 25 characters.");
                uname.focus();
                return false;
            }
            else {
                return true;
            }
        }

        function emailVal(uemail) {
            var uemail_len = uemail.value.length;
            if (uemail_len == 0) {
                alert('Email is required');
                return false;
            }
            else {
                return true;
            }
        }

        function ageNum(uage) {
            var age = uage.value;
            if (age == 0 || age < 18 || age > 100) {
                alert("Age is required. Between 18 and 100.");
                uage.focus();
                return false;
            }
            else
                return true;
        }


        function conSelect(ucon) {
            if (ucon.value == 0) {
                alert('Select your continent from the list');
                ucon.focus();
                return false;
            }
            else {
                return true;
            }
        } 

    </script>
    <center>
        <img src="Images/HorizonHomePageTitle.png" alt="Horizon Bed and Breakfast Title"></center>
</head>
<body>
    <center>
        <a href="XML/BudgetRooms.xml" target="_blank">Budget Rooms</a><center>
            <hr color="blue" width="60%">
            <center>
                <a href="XML/LuxuryRooms.xml" target="_blank">Luxury Rooms</a><center>
                    <hr color="blue" width="60%">
                    <center>
                        <a href="Index.html" target="_blank">Home</a><center>
                            <hr color="blue" width="60%">
                            <center>
                                <a href="https://www.visser.com.au/blog/generic-privacy-policy-for-australian-websites/"
                                    target="_blank">Our privacy policy</a><center>
                                        <hr color="blue" width="60%">
                                        <center>
                                            <a href="https://media.termsfeed.com/pdf/terms-and-conditions-template.pdf" target="_blank">
                                                Our terms of conditions</a><center>
                                                    <hr color="blue" width="60%">
                                                    <center>
                                                        <a href=" http://www.blogtyrant.com/best-about-us-pages/" target="_blank">Our terms
                                                            and conditions</a><center>
                                                                <hr color="blue" width="60%">
                                                                <center>
                                                                    <a href="http://www.blogtyrant.com/best-contact-us-pages/" target="_blank">Contact us</a><center>
                                                                        <form id='CustomerDetailForm' action="">
                                                                        <!-- ============================== Fieldset 1 - includes personal information of clients ============================== -->
                                                                        <fieldset>
                                                                            <table>
                                                                                <tr>
                                                                                    <td colspan="2">
                                                                                        <legend>Personal Information:</legend>
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td>
                                                                                        <label for="Full Name">
                                                                                            <strong>Full Name:*</strong></label>
                                                                                    </td>
                                                                                    <td>
                                                                                        <input id="FullNm" name="FullNm" type="text" size="20" class="text" />
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td>
                                                                                        <label for="Email">
                                                                                            <strong>Email:</strong></label><br />
                                                                                    </td>
                                                                                    <td>
                                                                                        <input id="Email" name="Email" type="text" size="20" class="text" />
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td>
                                                                                        <label for="Age">
                                                                                            <strong>Input age if between 18 and 100:</strong></label>
                                                                                    </td>
                                                                                    <td>
                                                                                        <input name="Ages" type="text" size="20" id="Ages" class="text" />
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td>
                                                                                        <label for="select" class="choose">
                                                                                            <strong>On what continent do you live?</strong></label>
                                                                                    </td>
                                                                                    <td>
                                                                                        <select id="select1" name="select">
                                                                                            <option value="0">Select</option>
                                                                                            <option value="1">Africa</option>
                                                                                            <option value="2">Europe</option>
                                                                                            <option value="3">Asia</option>
                                                                                            <option value="4">North Amrica</option>
                                                                                            <option value="5">South America</option>
                                                                                            <option value="6">Ausralia</option>
                                                                                        </select>
                                                                                    </td>
                                                                                </tr>
                                                                            </table>
                                                                        </fieldset>
                                                                        <!-- ============================== Fieldset 2 - Includes further optional information from clients to help productivity ============================== -->
                                                                        <fieldset>
                                                                            <legend>Questions:</legend>
                                                                            <hr class="form" />
                                                                            <label for="select2" class="choose">
                                                                                How long was your stay?</label><br />
                                                                            <select id="select" name="select2">
                                                                                <option value="7">Less than one week</option>
                                                                                <option value="8">More than one week</option>
                                                                                <option value="9">Months/years</option>
                                                                            </select>
                                                                            <label for="select3" class="choose">
                                                                                How many stars would you rate our BnB?</label><br />
                                                                            <select id="selectCon" name="select3">
                                                                                <option value="10">One star</option>
                                                                                <option value="11">Two star</option>
                                                                                <option value="12">Three star</option>
                                                                                <option value="13">Four star</option>
                                                                                <option value="14">Five star</option>
                                                                            </select>
                                                                        </fieldset>
                                                                        <!-- ============================== Fieldset 3 - Holds the comments section, aggreement radio button, submit button and a date button for the client  ============================== -->
                                                                        <fieldset>
                                                                            <legend>Further commetns:</legend>
                                                                            <hr class="form" />
                                                                            <textarea name="message" id="message" cols="20" rows="10"></textarea><br />
                                                                            <label for="protection" class="spam-protection">
                                                                                Spam check: 1 + 1=</label><br />
                                                                            <input name="ochrana" type="text" id="protection" class="answer" /><br />
                                                                            Do you agree to our terms?*<br>
                                                                            <input type="radio" name="Agreement" id="Agree">Agree<br>
                                                                            <button onclick="formValidation();">
                                                                                SUBMIT</button>
                                                                        </fieldset>
                                                                        </form>
                                                                        <script type="text/javascript">
                                                                            function submitFunction() {
                                                                                alert("Thankyou for your submission");
                                                                            }
                                                                        </script>
                                                                        <button type="button" onclick="document.getElementById('date/time').innerHTML = Date()">
                                                                            Display date and time.</button>
                                                                        <p id="date/time">
                                                                        </p>
</body>
</html>

关于JavaScript 无法验证 HTML 中的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40176202/

相关文章:

每个 <g> 的 CSS 动画 'origin'

javascript - 如何根据可拖动助手精确更改 svg 路径的控制点

javascript - 如何在用户关闭浏览器时从mysql数据库中删除一条记录

javascript - 如何检查 jqueryUI 自动完成下拉框是否打开

python - 如何在 Python 中解析 RSS feed 中的 HTML 标签

javascript - 需要元素在页面刷新时识别它在页面上的位置

javascript - 如何将变量重置回默认值?

javascript - 比较鼠标悬停时的 URL 以使用 javascript 显示下拉菜单

html - Bootstrap div,图像旁边是另一个 div

html - 垂直对齐表格单元格中的图像和文本区域