jQuery验证以比较开始日期和结束日期

jQuery验证以比较开始日期和结束日期

本文介绍了jQuery验证以比较开始日期和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个领域.一个用于person_start_date,另一个用于person_end_date.两者都有用于年,月,日的三个单独字段.我想验证人员结束日期">人员开始日期".预先感谢.

I have two fields. One for person_start_date and another for person_end_date. Both has three separate fields for year, month, date. I would like to validate like Person end date > Person start date. Thanks in advance.

html.erb

<%= datetime_select :person_start_date %>


   <select id="person_start_date_1i" name="person[start_date(1i)]"> ... </select>
   <select id="person_start_date_2i" name="person[start_date(2i)]"> ... </select>
   <select id="person_start_date_3i" name="person[start_date(3i)]"> ... </select>

html.erb

<%= datetime_select :person_end_date %>


  <select id="person_end_date_1i" name="person[end_date(1i)]"> ... </select>
  <select id="person_end_date_2i" name="person[end_date(2i)]"> ... </select>
  <select id="person_end_date_3i" name="person[end_date(3i)]"> ... </select>

推荐答案

以下是一个建议:

var startDate = new Date(
    $('#person_start_date_1i').val(),
    $('#person_start_date_2i').val() - 1,
    $('#person_start_date_3i').val()
);
var endDate = new Date(
    $('#person_end_date_1i').val(),
    $('#person_end_date_2i').val() - 1,
    $('#person_end_date_3i').val()
);
if (endDate > startDate) {
    // do something
}

这篇关于jQuery验证以比较开始日期和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 00:59