我正在为我的雇主编辑一个HTML表单,其中有一个出生日期字段集,有三个选择框:“日”,“月”,“年”。提交表格不需要出生日期。但是,他们需要我创建一个表单验证,使用户无法在三个选择框中的一个或两个中选择选项。换句话说,如果选择了出生日期字段集中的任何内容,则必须为表单选择所有内容以进行验证。如果用户试图仅选择Day而不是Month或Year,他们希望所有三个选择框在提交时更改为红色边框和不同的颜色背景,并且提交按钮将被禁用,直到所有选择都已制作或清除。他们还希望在选择框下显示一条错误消息,但javascript已经存在,可以正常运行。
我仍然是javascript的新手,并且无法在网上或本网站上找到解决方案。我看到了其他形式验证器的例子,但没有一个似乎做我想做的事。
以下是我正在使用的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 -->
提前感谢你能给我的任何指示。
*我尝试了所提供的两个答案,但没有让它发挥作用。所以我编辑了我的问题,添加了我正在使用的javascript。不确定在哪里添加css样式更改。其他所有内容(错误消息和禁用的提交按钮)都正常工作:
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;
}
答案 0 :(得分:2)
您可以在表单提交
上添加此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()">
答案 1 :(得分:0)
我看到你用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();
});
});