Javascript Data Type How to - Check date range validation








Question

We would like to know how to check date range validation.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from w  w w .j  av a  2s .  c  om-->
$('#startDate').change(function() {
   checkMyDateWithinRange(new Date($(this).val()))     
});
$('#endDate').change(function() {
   checkMyDateWithinRange(new Date($(this).val()))     
});
function checkMyDateWithinRange(myDate) {
    var startDate = new Date(2012, 11, 1);
    var endDate = new Date(2013, 0, 1);
    if (startDate <= myDate && myDate <= endDate) {
        document.writeln('Date is in Range');
    }
    else     
     document.writeln('Date is not in Range');
}
});
</script>
</head>
<body>
  Enter Date From 1st December 2012 to 1st Jan 2013
  <br /> Start Date(i.e, year/month/date):
  <input type="text" id="startDate" />
  <br /> End Date:(i.e, year/month/date)
  <input type="text" id="endDate" />
</body>
</html>

The code above is rendered as follows: