简体   繁体   English

如何验证表单字段集是否已选中所有(如果有的话)选择框?

[英]How to validate that a form fieldset has all, if any, select boxes selected?

I'm editing an HTML form for my employer that has a Date of Birth fieldset with three select boxes: "Day", "Month", "Year". 我正在为我的雇主编辑一个HTML表单,该表单的“出生日期”字段集带有三个选择框:“天”,“月”,“年”。 The date of birth is not required to submit the form. 出生日期不需要提交表格。 However, they need me to create a form validation that makes it so a user cannot select options in just one or two out of the three select boxes. 但是,他们需要我创建一个表单验证,使用户无法在三个选择框中仅选择一个或两个来选择选项。 In other words, if anything in the Date of Birth fieldset is selected, they must all be selected for the form to validate. 换句话说,如果在“出生日期”字段集中选择了任何内容,则必须全部选择所有内容以进行验证。 And if a user tries to select, say, just the Day and not the Month or Year, they want all three of the select boxes to change to a red border and different color background on submit, and for the submit button to be disabled until all selections are made or cleared. 并且,如果用户尝试仅选择“日期”而不是“月”或“年”,则他们希望所有三个选择框在提交时都变为红色边框和不同的颜色背景,并禁用提交按钮,直到所有选择均已完成或清除。 They also want an error message to display under the select boxes, but the javascript was already there to make that function properly. 他们还希望在选择框下显示一条错误消息,但是javascript已经可以正常使用了。

I'm still pretty new to javascript and haven't been able to find the solution anywhere online or on this site. 我对javascript还是很陌生,还无法在线或在此站点上找到任何解决方案。 I saw other examples of form validators, but none that seemed to be do what I'm looking to do. 我看到了其他形式验证器的示例,但似乎没有一个可以满足我的期望。

Here's a shortened version of the HTML code I'm working with that came from the development team: 这是我正在使用的HTML代码的简化版本,来自开发团队:

<div class="control-group">
<fieldset>
    <legend id="dateofbirth">Date of Birth:</legend>
    <div class="controls">
        <div class="hide"><label tabindex="0" id="dd">Select your two-digit day of birth.</label></div>
        <select id="dob-dd-select" name="BIQ_DOB_DAY" class="dd" aria-required="true">
            <option id="dob-day-0" role="option" aria-selected="true" value="">Day</option>
            <option id="dob-day-1" role="option" aria-selected="false" value="1">1</option>
            <option id="dob-day-2" role="option" aria-selected="false" value="2">2</option>
        </select>

        <div class="hide"><label id="mm">Select your month of birth.</label></div>
        <select class="mm" id="dob-month-select" name="BIQ_DOB_MONTH" tabindex="0" aria-required="true">
            <option id="dob-month-0" role="option" aria-selected="true" value="">Month</option>
            <option id="dob-month-1" role="option" aria-selected="false" value="1">1. January</option>
            <option id="dob-month-2" role="option" aria-selected="false" value="2">2. February</option>
        </select>

        <div class="hide"><label tabindex="0" id="yyyy">Select your four-digit birth year.</label></div>
            <select class="yyyy" id="dob-yyyy-select" name="BIQ_DOB_YEAR" tabindex="0" aria-required="true">
                <option id="yyyy0" role="option" aria-selected="true" value="">Year</option>
                <option id="dob-year-1" role="option" aria-selected="false" value="1900">1900</option>
                <option id="dob-year-2" role="option" aria-selected="false" value="1901">1900</option>
            </select>
    </div><!--/controls-->
</fieldset>
</div><!--/control-group-->

<div>
<button tabindex="0" onclick="validateDateOfBirthDropDownOnSubmit();" type="button" id="btn-biq-submit">Continue</button>
</div><!-- /submit button -->

Thanks in advance for any pointers you can give me. 预先感谢您可以给我的任何指导。

*I attempted both of the answers provided, but didn't get it to work. *我尝试了提供的两个答案,但没有成功。 So I edited my question to add the javascript I'm working with. 所以我编辑了问题以添加正在使用的javascript。 Not sure where to add the css style changes. 不知道在哪里添加CSS样式更改。 Everything else (error message and disabled submit button) are working properly: 其他所有内容(错误消息和禁用的提交按钮)均正常运行:

var validateDateOfBirthDropDown = function() {
            console.log("validateDateOfBirthDropDown start");
            var allPopulated = false;
            var allValid = false;
            var onePopulated = false;
            var txtMonthPopulated = false;
            var txtDatePopulated  = false;
            var txtYearPopulated  = false;
            //console.log(" biq_dob_day val " + $('#dob-month-select').val());
            txtMonthPopulated = checkSelectPopulated($('#dob-month-select').val());
            //console.log(" biq_dob_month val " + $('#dob-dd-select').val());
            txtDatePopulated = checkSelectPopulated($('#dob-dd-select').val());
            //console.log(" biq_dob_year val " + $('#dob-yyyy-select').val());
            txtYearPopulated = checkSelectPopulated($('#dob-yyyy-select').val());
            console.log(" txtMonthPopulated val " + txtMonthPopulated + ", txtDatePopulated "+txtDatePopulated+", txtYearPopulated " +txtYearPopulated);
            if(txtMonthPopulated && txtDatePopulated && txtYearPopulated){
                    console.log("all populated");
                    allPopulated = true;
            }
            if(txtMonthPopulated || txtDatePopulated || txtYearPopulated){
                    console.log("atleast one populated");
                    onePopulated = true;
            }
            if(!txtMonthPopulated && !txtDatePopulated && !txtYearPopulated){
                    console.log("all not populated");
                    allPopulated = false;
            }
            if(allPopulated){
                    console.log("all populated");
                    //hideDateOfBirthError();
                    return true;
            }
            if(onePopulated && !allPopulated){
                    //partial error
                    console.log("partial - onePopulated && !allPopulated");
                    //showDateOfBirthError();
                    return false;
            }
            if(!allPopulated){
                    console.log("!allPopulated");
                    //hideDateOfBirthError();
                    return true;
            }
            console.log("validateDateOfBirthDropDown complete");
    }
    var checkSelectPopulated = function(valLocal){
            if(valLocal == "" || valLocal == "notselected"){
                    return false;
            }
            return true;

} }

you can add this javascript code on form submit 您可以在表单提交中添加此javascript代码

<script>
function checkdate()
{
var x1=document.forms["form-name"]["dob-dd-select"].value;
var x2=document.forms["form-name"]["dob-month-select"].value;
var x3=document.forms["form-name"]["dob-yyyy-select"].value;
if !(x1==null || x1=="" || x2==null || x2=="" || x3==null || x3=="")
  {
  alert("Date must be filled out");
  return false;
  }

</script>


<form name="" id="" onsubmit="return checkdate()">

I see you tagged the question with jQuery, so you can work something around these lines: 我看到您使用jQuery标记了问题,因此您可以围绕以下几行工作:

$(function () {
    // Assuming there's just one form on the page.
    // If not, just change the selector to an appropriate one.
    $("form").on("submit", function (e) {
        // Stop form submission.
        e.preventDefault();

        // Check select values.
        var dayValid = $("#dob-dd-select").val() != "";
        var monthValid = $("#dob-month-select").val() != "";
        var yearValid = $("#dob-yyyy-select").val() != "";

        // Play with the layout
        if (!dayValid)
            $("#dob-dd-select").css("border-color", "red");

        if (!monthValid)
            $("#dob-month-select").css("border-color", "red");

        if (!yearValid)
            $("#dob-yyyy-select").css("border-color", "red");

        // Something is not right, return to the form.
        if (!dayValid || !monthValid || !yearValid)
            return false;

        // The form is valid, so submit it.
        $(this).submit();
    });
});

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

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