我正在进行日期验证,我希望我的日期应该少于jquery验证插件中的日期。
我发布了我的代码。
<form id="projectDetails" role="form" class="errormessage form-horizontal" method="post" action="<?php echo base_url();?>profile/addprojectdetails" enctype="multipart/form-data">
<div class="form-group clearfix">
<label for="status" class="col-sm-3 control-label">Duration</label>
<div class="p5 col-md-2 pull-left mr10">
<select id="from_month_attended" name="from_month" class="small_input form-control">
<option value="0">Month</option>
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option value="Mar">Mar</option>
<option value="Apr">Apr</option>
<option value="May">May</option>
<option value="Jun">Jun</option>
<option value="Jul">Jul</option>
<option value="Aug">Aug</option>
<option value="Sep">Sep</option>
<option value="Oct">oct</option>
<option value="Nov">Nov</option>
<option value="Dec">Dec</option>
</select>
</div>
<div class="p5 col-md-2 pull-left">
<select id="from_year_attended" name="from_year" class="small_input form-control">
<option value="0">year</option>
<?php
for( $y=2014; $y>=1921; $y-- )
{
echo "<option value='".$y."'>".$y."</option>";
}
?>
</select>
</div>
<span class="pull-left">-</span>
<div class="p5 col-md-2 pull-left">
<select id="to_month_attended" name="to_month" class="small_input form-control">
<option value="0">Month</option>
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option value="Mar">Mar</option>
<option value="Apr">Apr</option>
<option value="May">May</option>
<option value="Jun">Jun</option>
<option value="Jul">Jul</option>
<option value="Aug">Aug</option>
<option value="Sep">Sep</option>
<option value="Oct">oct</option>
<option value="Nov">Nov</option>
<option value="Dec">Dec</option>
</select>
</div>
<div class="p5 col-md-2 pull-left" id="insertDateError">
<select onchange="attendYear();" id="to_year_attended" name="to_year" class="small_input form-control">
<option value="0">Year</option>
<?php
for( $y=2014; $y>=1921; $y-- )
{
echo "<option value='".$y."'>".$y."</option>";
}
?>
</select>
</div>
</div>
</form>
脚本:
function attendYear(){
var att_from_month=$('#from_month_attended').val();
var att_from_year=$('#from_year_attended').val();
var att_from = att_from_month+'-'+att_from_year;
var att_to_month=$('#to_month_attended').val();
var att_to_year=$('#to_year_attended').val();
var att_to = att_to_month+'-'+att_to_year;
if(att_from_month !=0 && att_from_year!=0 && att_to_month!=0 && att_to_year!=0)
{
var from = att_from;
var to = att_to;
if ((new Date(from).getTime()) <= (new Date(to).getTime())) {
document.getElementById("errordate").innerHTML = "";
} else {
document.getElementById("errordate").innerHTML = "Fromdate should less than todate";
}
}
}
我尝试了这段代码,它在谷歌浏览器中运行良好,但它不能在Firefox中运行。任何人都可以帮我解决错误。
答案 0 :(得分:0)
您可以尝试这种简单的方法(显然它不是更好的解决方案)并在验证器功能中添加所需的规则和消息。
var fromMonth= $('#duration_from_month').val();
var fromYear= $('#duration_from_year').val();
var toMonth= $('#duration_to_month').val();
var toYear= $('#duration_to_year').val();
if(toYear<fromYear){
....do something
}
else {
if(toMonth<fromMonth){
....do something
}
}