Check if date is in the past without submitting form

前端 未结 6 1378
抹茶落季
抹茶落季 2020-12-19 08:44

I have this code that I want to check if a date is in the past. I want to check it as soon as the date is entered, before form submission.



        
6条回答
  •  既然无缘
    2020-12-19 09:09

    All you need to do is convert the string produced by the into a Date using the Date constructor new Date("2014-06-12")

     function checkDate() {
       var selectedText = document.getElementById('datepicker').value;
       var selectedDate = new Date(selectedText);
       var now = new Date();
       if (selectedDate < now) {
        alert("Date must be in the future");
       }
     }

提交回复
热议问题