简体   繁体   English

验证-1个字段不大于另一个字段

[英]Validation - 1 Field not greater than another

I am using 2 jquery datepickers. 我正在使用2 jquery datepickers。 When the values of the datepickers are equal - I want to prevent Time 2 from being larger than Time 1. The times are 24hr HHMM ( 4 character ) textboxes. 当日期选择器的值相等时-我想防止时间2大于时间1。时间为24小时HHMM(4个字符)文本框。

When the datepickers are not equal, anything goes for the Times.... 当日期选择器不相等时,《泰晤士报》无所不能。

I have it kind of working, but its not exact... I can change the fields and get around the validation which I dont want.... 我有它的工作,但不完全是...我可以更改字段并避开不需要的验证...。

Also dont know if "focusout" is the way to go... 还不知道“聚焦”是不是要走的路...

http://jsfiddle.net/rbla/90ps4h9n/5/ http://jsfiddle.net/rbla/90ps4h9n/5/

html... html ...

  Start Date: <input type="text" id="StartDate" name="datepicker1" value="" />
  End Date: <input type="text" id="EndDate" name="datepicker2" value="" />

  <br/><br/>

  Time 1: <input type="text" id="StartTime" maxlength="4">
  Time 2: <input type="text" id="EndTime" maxlength="4">

  <div class="error" style="display:none">Time 1 needs to be less than Time 2<div>

javascript... javascript ...

  $(function() {
      $("#StartDate").datepicker({
      dateFormat: "dd-M-yy"
      });
      $("#EndDate").datepicker({
      dateFormat: "dd-M-yy"
     });
  });

  $("#EndDate").change(function () {
      var sDate = document.getElementById("StartDate").value;
      var eDate = document.getElementById("EndDate").value;

      if ((Date.parse(eDate) === Date.parse(sDate))) {

          $("#EndTime").focusout(function(){

          if(parseFloat($("#StartTime").val()) > parseFloat($("#EndTime").val()))
              {
              $(".error").css("display","block").css("color","red");
              $("#submit").prop('disabled',true);
              document.getElementById("EndTime").value = "";
              }
              else 
              {
              $(".error").css("display","none");
              $("#submit").prop('disabled',false);        
              }
         });
     }
 });

I think you can compare two objects of date Date('date') by comparison operator 我认为您可以通过比较运算符比较日期为Date('date')的两个对象

if(parseFloat($("#StartTime").val()) > parseFloat($("#EndTime").val()))

replace 更换

if(new Date($("#StartTime").val()) > new Date($("#EndTime").val()))

I am not sure I have made exact change as you need. 我不确定我是否已根据您的需要进行了准确的更改。 but I have give you idea about date object comparision. 但是我给你关于日期对象比较的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM