Check if date is in the past without submitting form

前端 未结 6 1375
抹茶落季
抹茶落季 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:19

    For those who use type="date" (introduced with HTML5) instead of type="text", it can be done this way:

     function checkDate() {
       var date = this.value;
       if(date.valueAsDate <= new Date()) {
           //Date in the past
       } else {
           //Date in the future
       }
    }
    

提交回复
热议问题