I'm trying to test to make sure a date is valid in the sense that if someone enters 2/30/2011 then it should be wrong.
2/30/2011
How can I do this with any date?
One simple way to validate a date string is to convert to a date object and test that, e.g.
// Expect input as d/m/y function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2], bits[1] - 1, bits[0]); return d && (d.getMonth() + 1) == bits[1]; } ['0/10/2017','29/2/2016','01/02'].forEach(function(s) { console.log(s + ' : ' + isValidDate(s)) })
2.1m questions
2.1m answers
60 comments
57.0k users