JS判斷開始時間小於結束時間案例
阿新 • • 發佈:2018-12-03
一.html
預計入住時間:<input class="pub_textAlign_right" placeholder="例如,${memoDate }" type="text" name="preLiveInDate" id="preLiveInDate" maxlength="50" readonly="readonly"> 預計離開時間:<input class="pub_textAlign_right" placeholder="(長期入住可不填)" type="text" name="preLeaveOutDate" id="preLeaveOutDate" maxlength="50" readonly="readonly">
二.js驗證
function dateChange(){ var endtime = $('#preLeaveOutDate').val(); var starttime = $('#preLiveInDate').val(); if(endtime!=""&&starttime!=""){ var start = new Date(starttime.replace("-", "/").replace("-", "/")); var end = new Date(endtime.replace("-", "/").replace("-", "/")); if(end < start){ alert("預計離開時間不能小於預計入住時間!"); $('#preLeaveOutDate').val(""); $('#preLiveInDate').val(""); } } }