I am trying to develop a system which checks if the date is entered by the user or not, however when ever i try to use these if(!date){//code}
or if(date.length==0)
i don't get what i want, maybe because <input type="date">
execute input feild with dd-----yyyy and a celender in the browser which is counted as a value in js.
So my question is to get 1 if the date is entered by the user and 0 if user leaves the field empty or default ( dd-----yyyy and a celender ).
You Have To Use
if(date.value.length == 0)
Not
if(date.length==0)
var f = document.querySelector("form"); f.addEventListener("submit", function(e){ var d = document.getElementById("date"); if(d.value.length < 1){ alert("Date Is Required"); e.preventDefault(); } });
<form> <input type="date" id="date" name="date"> <button>Go</button> </form>
Or Try This way
var f = document.querySelector("form"); f.addEventListener("submit", function(e){ const d = document.getElementById("date"); if(!d.checkValidity()){ alert("Date Is Required"); e.preventDefault(); } });
<form novalidate> <input type="date" id="date" name="date" required> <button>Go</button> </form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.