jQuery低音强度:验证结束日期在开始日期之后 [英] Jquery bassistance: validate that end date is after start date

查看:121
本文介绍了jQuery低音强度:验证结束日期在开始日期之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery低音强度验证,需要验证开始日期是否在结束日期之后.我想知道是否有人知道此功能是否存在,是否可以添加规则来执行此操作,或者是否必须添加自定义检查.我很难找到可以与我的代码一起使用的任何示例,如果表单有效,该示例可转发到另一个函数. (粘贴在下面)

I am using jquery bassistance validation and need to verify that a start date is after an end date. I am wondering if anyone knows if this functionality exists and if I can add rules to do this or if I have to add a custom check. I am having difficulty finding any examples of this that work with my code which forwards to another function if the form is valid. (pasted below)

$("#hotels").validate({
    rules: {
        checkinDate: {
            date: true
        },
        checkoutDate: {
            date: true
        }
    }
});


$("#hotels input.submit").click(function() {
  var isValid = $("#hotels").valid();
  if (isValid == true) {
      } else {
          hComp();
          return false;
      }
  } else {
      return false;
  }
});

我在另一个示例中看到了这一点,但不确定如何使用它.

I saw this in another example, but am not sure how I could use it.

var startDate = new Date($('#startDate').val());
var endDate = new Date($('#endDate').val());

if (startDate < endDate){
// Do something
}

在此先感谢您的帮助.

推荐答案

该示例似乎正是您所需要的.在这里.

That example looks like it's exactly what you need. Have a look here.

借用该jquery示例,假设您具有以下HTML:

Borrowing from that jquery example, suppose you have the following HTML:

  <input id="startDate" value="Jan 12, 2009" />
  <input id="endDate" value="Jan 12, 2010" />

然后,此JavaScript代码应显示警告开始日期早于结束日期!". (假设值未修改):

Then this JavaScript code should show the alert "Start date is before end date!" (assuming the values aren't modified):

var startDate = new Date($('#startDate').val());
var endDate = new Date($('#endDate').val());

if (startDate < endDate){
    alert("Start date is before end date!");
}

这篇关于jQuery低音强度:验证结束日期在开始日期之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆