Javascript - date range validation

前端 未结 2 516
清酒与你
清酒与你 2021-01-23 08:36

i\'ve a form user can enter any date , but i want to set a date range validation . for example: from 1-12-2012 to 1-1-2013 and the system can\'t accept any date from user that n

2条回答
  •  抹茶落季
    2021-01-23 08:53

    You can make a function like this:

    function checkMyDateWithinRange(myDdate){
        var startDate = new Date(2012, 11, 1);
        var endDate = new Date(2012, 0, 1);     
        if (startDate < myDate && myDate < endDate) {
           return true; 
        }
       return false;
    }
    

    and test any date like calling this function:

    var inputDate= document.getElementById('tbDate'),
    date = new Date(inputDate.value);
    if(!checkMyDateWithinRange(date)){
        alert('Date is not in range!!!');
    }
    

    Here is Working Demo

提交回复
热议问题