When a user selects a date on input type="date", then the value property of that input element will have a date as "YYYY-MM-DD". On the other hand, if a user does not select a date, then the value property is empty.
You can use this information to determine if the input type="date" is empty or not.
Here is the HTML code as an example.
<!DOCTYPE html> <html> <head> <title>Check input type="date" is empty</title> </head> <body> <input type="date" id="date-of-birth" name="date-of-birth" /> <button id="btn">Submit</button> <script type="text/javascript" src="index.js"></script> </body> </html>
JavaScript code
let btn = document.getElementById("btn"); btn.addEventListener('click', (event)=>{ let inputDate = document.getElementById('date-of-birth'); if(!inputDate.value){ console.log("Input type date is empty."); }else{ console.log("Input type date is not empty."); } });
Here, is the explanation of the code: