我正在尝试验证我的出生日期。
基本上,当输入第一个和第二个dob时,它工作正常,但在最后一个选择年,它验证了不工作,并返回true。
这是代码:
function dobvalidation()
{
var dob1=document.getElementById('dob_input1').value;
var dob2=document.getElementById('dob_input2').value;
var dob3=document.getElementById('dob_input3').value;
if(dob1=='' || dob1==null && dob2=='' || dob2==null && dob3=='' || dob3==null)
{
document.getElementById('headerErrorBox').style.display='block';
document.getElementById('top-dob').style.display='block';
document.getElementById('dob_error_msg').style.display='block';
document.getElementById('below_error_dob_msg').style.display='block';
document.getElementById('dob_icon').style.marginLeft='-58px';
document.getElementById('dob_success_msg').style.display='none';
return false;
}
}
<div id="headerErrorBox" style="display: none;">Error!</div>
<div id="dob_success_msg" style="display: none;">Success!</div>
<select name="dob_day" onchange="return dobvalidation()" id="dob_input1" class="frmSelect" autocomplete="off" style="width: 30%;">
<option>DD</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">so on.. to 30</option>
</select>
<select name="dob_mm" onchange="return dobvalidation()" id="dob_input2" class="frmSelect" autocomplete="off" style="width: 25%">
<option>MM</option>
<option value="January">January</option>
<option value="Febuary">Febuary</option>
<option value="March">so on. untill dec</option>
</select>
<select name="dob_day" onchange="return dobvalidation()" id="dob_input3" class="frmSelect" autocomplete="off" style="width: 30%;">
<option value="">YY</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
</select>
将上一个<select>
标记名<select name="dob_day"...
更改为<select name="dob_year"...
或<select name="whatever_you_want_to_name_it"...
,但与第一个<select>
标记中已定义的name=""
不同。
编辑:根据JS函数,这将在上一个<select>
标记中返回一个空值错误。
<option value="">YY</option>
用<option>YY</option>
替换
您的条件应该有OR而不是AND,这样,如果没有任何值,它应该返回false。
function dobvalidation()
{
var dob1=document.getElementById('dob_input1').value;
var dob2=document.getElementById('dob_input2').value;
var dob3=document.getElementById('dob_input3').value;
if(dob1=='' || dob1==null || dob2=='' || dob2==null || dob3=='' || dob3==null)
{
document.getElementById('headerErrorBox').style.display='block';
document.getElementById('dob_success_msg').style.display='none';
return false;
}
}
<div id="headerErrorBox" style="display: none;">Error!</div>
<div id="dob_success_msg" style="display: none;">Success!</div>
<select name="dob_day" onchange="return dobvalidation()" id="dob_input1" class="frmSelect" autocomplete="off" style="width: 30%;">
<option>DD</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">so on.. to 30</option>
</select>
<select name="dob_mm" onchange="return dobvalidation()" id="dob_input2" class="frmSelect" autocomplete="off" style="width: 25%">
<option>MM</option>
<option value="January">January</option>
<option value="Febuary">Febuary</option>
<option value="March">so on. untill dec</option>
</select>
<select name="dob_day" onchange="return dobvalidation()" id="dob_input3" class="frmSelect" autocomplete="off" style="width: 30%;">
<option value="">YY</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
</select>
我在选择DD和MM中缺少值,这就是它不起作用的原因。
function dobvalidation()
{
var dob1=document.getElementById('dob_input1').value;
var dob2=document.getElementById('dob_input2').value;
var dob3=document.getElementById('dob_input3').value;
if(dob1=='' || dob1==null , dob2=='' || dob2==null, dob3=='' || dob3==null)
{
document.getElementById('headerErrorBox').style.display='block';
document.getElementById('top-dob').style.display='block';
document.getElementById('dob_error_msg').style.display='block';
document.getElementById('below_error_dob_msg').style.display='block';
document.getElementById('dob_icon').style.marginLeft='-58px';
document.getElementById('dob_success_msg').style.display='none';
return false;
}
}
<div id="headerErrorBox" style="display: none;">Error!</div>
<div id="dob_success_msg" style="display: none;">Success!</div>
<select name="dob_day" onchange="return dobvalidation()" id="dob_input1" class="frmSelect" autocomplete="off" style="width: 30%;">
<option value=''>DD</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">so on.. to 30</option>
</select>
<select name="dob_mm" onchange="return dobvalidation()" id="dob_input2" class="frmSelect" autocomplete="off" style="width: 25%">
<option value=''>MM</option>
<option value="January">January</option>
<option value="Febuary">Febuary</option>
<option value="March">so on. untill dec</option>
</select>
<select name="dob_day" onchange="return dobvalidation()" id="dob_input3" class="frmSelect" autocomplete="off" style="width: 30%;">
<option value="">YY</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
</select>