JSFiddle - React, Tailwind, and code Playground
by esbenp
HTML
<input type="text" name="date">
<input type="submit" name="check">
JavaScript
$(document).ready(function() {
$("input[name=check]").click(function() {
alert(isValidDate($("input[name=date]").val()));
});
});
function isValidDate(d) {
var pattern = /^(\d{2})-(\d{2})-(\d{4})$/,
matches = pattern.exec(d),
day = parseInt(matches[1]),
month = parseInt(matches[2]),
year = parseInt(matches[3]);
if (day > 31) {
return false;
} else if (day === 31) {
if (month % 2 === 0) {
return false;
}
}
if (month === 2) {
var leapyear = false;
if (isInt((year/4))) {
leapyear = true;
}
if (isInt((year/100))) {
leapyear = false;
}
if (isInt((year/400))) {
leapyear = true;
}
if (day > 29) {
return false;
} else if (day === 29) {
if (leapyear === false) {
return false;
}
}
}
return true;
}
function isInt(n) {
return n % 1 == 0;
}