简体   繁体   English

使用jQuery,第一个日期字段不应大于第二个日期字段值

[英]first date field should not be greater than second date field value using jquery

Currently working on jQuery date with my current code where i can able to select the date in both the fields I want to check if user select the passport date should not be greater than expiry date. 目前使用我当前的代码在jQuery日期上工作,我可以在这两个字段中选择日期,我想检查用户是否选择护照日期不大于到期日期。

This is my current jquery code 这是我当前的jQuery代码

    function testDates() {
    var from = new Date(Date.parse($("#txt_Idt").attr("value")));
    var to = new Date(Date.parse($("#txt_Epdt").attr("value")));
    if (from > to) {
        alert("From is greater than to!");
        return;
    }
    // alert("do submit");
}

Here is the fiddle link 这是小提琴链接

DEMO 演示

Use onSelect option of datePicker [assuming jquery-ui datepicker from the fiddle] and change minDate and maxDate of toDate and fromDate respectively as below and make your input readonly so that user input is prevented. 使用datePicker onSelect选项[假定小提琴中的jquery-ui datepicker datePicker ]并分别如下更改toDatefromDate minDatemaxDate ,并使您的输入为readonly以防止用户input

$(".txt_Idt").datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: 'mm/dd/yy',
    yearRange: '-115:+1M',
    maxDate: new Date(),
    onSelect: function(dateText) {
        $(".txt_Epdt").datepicker("option", "minDate", dateText);
    }
});
$(".txt_Epdt").datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: 'mm/dd/yy',
    yearRange: '-115:+95M',
    onSelect:function(dateText) {
        $(".txt_Idt").datepicker('option','maxDate',dateText);
    }
});

HTML 的HTML

<input type="text" placeholder="Passport Date" class="ipt_Field txt_Idt ipt_required" 
   id="txt_Idt" name="txt_Idt" readonly /> <!--Add readonly here-->
<input type="text" placeholder="Expiry Date" class="ipt_Field txt_Epdt ipt_required" 
   id="txt_Epdt" name="txt_Epdt" readonly /><!--Add readonly here-->

Use onSelect event of datepicker 使用日期选择器的onSelect事件

  $(document).ready(function() { $(".txt_Idt").datepicker({ changeMonth: true, changeYear: true, dateFormat: 'mm/dd/yy', yearRange: '-115:+1M', maxDate: new Date(), onSelect: function(dateText, inst) { var expDate = new Date($("#txt_Epdt").val()); var ppt = new Date(dateText); if (ppt > expDate) { alert(" Passport date is greater than Expiry Date!"); return; } else alert(dateText); } }); $(".txt_Epdt").datepicker({ changeMonth: true, changeYear: true, dateFormat: 'mm/dd/yy', yearRange: '-115:+95M', onSelect: function(dateText, inst) { var from = new Date($("#txt_Idt").val()); var to = new Date(dateText); if (from > to) { alert("From is greater than to!"); return; } else alert(dateText); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <input type="text" placeholder="Passport Date" class="ipt_Field txt_Idt ipt_required" id="txt_Idt" name="txt_Idt" /> <input type="text" placeholder="Expiry Date" class="ipt_Field txt_Epdt ipt_required" id="txt_Epdt" name="txt_Epdt" /> 

Here is a simple date comparision example using jQuery. 这是一个使用jQuery的简单日期比较示例。 Basically just use the Date object and then compare them: 基本上只使用Date对象,然后进行比较:

$(document).ready(function () {
    $('#date1').datepicker();
    $('#date2').datepicker();

    $('#date2').on('change', function () {
        var date1 = new Date($('#date1').val());
        var date2 = new Date($('#date2').val());
        console.log(date1);
        console.log(date2);

        if (date1 > date2) {
            alert("date1 is greater than date2");
        }
    });
});

暂无
暂无

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

相关问题 截止日期应大于jquery中的截止日期 - To date should be greater than from date in jquery jQuery datepicker根据第一个日期字段中的选定日期限制第二个日期字段中的日期,反之亦然 - Jquery datepicker restrict dates in second date field based on selected date in first date field and wise versa 输入字段值不应大于其他字段值 - Input field value should not be greater than other field value 开始日期不得大于结束日期jQuery PHP - Start date should not greater than End date jquery php Onkey up第二个输入值不应大于第一个值 - Onkey up Second input value should not be greater than first value 引导日期选择器:第二个字段,允许日期晚于第一个字段中的所选日期 - Bootstrap datepicker: second field to allow dates later than the selected date on first field 如何使用Jquery在动态创建的日期字段中添加日期值 - How to add date value in dynamically created date field using Jquery 我有两个输入数据选择器字段,第一个字段应该是 01/01/2022,第二个字段应该是当前日期 - I have two input data picker fields, in first field it should be like 01/01/2022 and in the second field should have current date Jquery日期选择器:选择一个日期,自动更新第二个字段 - Jquery Date picker: Select one date, automatically update second field jQuery选择值大于零的最后一个字段 - Jquery Select last field having Value greater than Zero
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM