Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  116] [ 2]  / answers: 1 / hits: 80747  / 11 Years ago, tue, october 1, 2013, 12:00:00

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!


More From » input

 Answers
21

You could check for a falsy value:


if (!valueDate) {
// ...
}

The falsy values in JavaScript are:



  1. undefined

  2. null

  3. false

  4. ""

  5. 0 and -0

  6. 0n

  7. 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.


[#75314] Monday, September 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darleneh

Total Points: 231
Total Questions: 110
Total Answers: 94

Location: Spain
Member since Thu, Dec 23, 2021
3 Years ago
darleneh questions
;