I want to check if the user entered a date. But I can't figure it out how to do it.
Here's some javascript code what i already got but doesn't work :
var valueDate = document.getElementById('Date').value;
if ( valueDate== null || valueDate== '')
{
alert('Date is empty');
return false;
}
And the HTML <input type="date" name="Date" id="Date"/>
Thanks in advance!
Assuming you want to make the value shown to dd/mm/yyyy , then click on Clear button.
"NULL" can be specified as a value in the Date field to get an empty/blank by using INSERT statement. Example: CREATE table test1 (col1 date); INSERT into test1 values (NULL);
You could check for a falsy value:
if (!valueDate) {
// ...
}
The falsy values in JavaScript are:
undefined
null
false
""
0
and -0
0n
NaN
Since document.getElementById('Date').value
is always of type string if a value is set, you don't get false positives like 0
being treated like no input, which would be the case if the type was number
.
I would try using Date.parse() if I were you.
var valueDate = document.getElementById('Date').value;
if(!Date.parse(valueDate)){
alert('date is invalid');
}
http://www.w3schools.com/jsref/jsref_parse.asp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With