开发者

How can I check that the date inputted is within 90 days?

I'm using jQuery's Dat开发者_高级运维epick maxDate option to limit the selection on the calendar to 90 days. But the user can override that and type in a date manually if they wanted to get around it. How can I check with javascript that the inputted date is within 90 days of today. The date format is in mm/dd/YYYY right now. Ex. 06/21/2011

<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"     type="text/javascript"></script> 
<input type="text" id="txtMaxDate" />
<input type="submit" />

$(function() {
$("#txtMaxDate").datepicker({
    maxDate: '+90d'
});
$('input:submit').click(function() {
    var today = new Date();
    var targetDate = $("#txtMaxDate").val();
    //$( "#txtMaxDate" ).datepicker( "option", "dateFormat", '@' );
    alert(today);
    alert(targetDate);

});
});

Example here - http://jsfiddle.net/6qmPP/1/


This seems to work nicely:

$(function() {
    $("#txtMaxDate").datepicker({
        maxDate: '+90d'
    });
    $('input:submit').click(function() {
        var today = new Date();
        var targetDate = $("#txtMaxDate").datepicker( "getDate");
        if(Math.round(Math.abs(today - targetDate) / (1000 * 60 * 60 * 24)) > 90) {
            alert("date is more than 90 days from today");
        }
    });
});

As others have said, make sure that you validate this on the server side.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜