Kamal Singh Kamal Singh - 2 months ago 4
Javascript Question

I need a javascript code that will check the valid values are entered in month and days depending on the type of year

I want some extra piece of code in this so that it will check for leap year and if it is it will allow 29 days in February month otherwise it should check for 28 days and if greater it should show an alert message.



<html>
<head>
<title>
Redirection using Delay
</title>

</head>

<body>
<h1>Enter Date in Valid Formats:</h1>
<input type="text" id="dat" name="date" />
<input type="submit" value="Go" name="submit" onclick="datecheck();" />
<script>
var dats;
function datecheck() {
debugger;
dats = document.getElementById("dat").value.split("/");
if (dats[1] > 12) {
alert("Invalid Date Format");
return false;
}
else {
if (dats[1] == 1 || dats[1] == 3 || dats[1] == 5 || dats[1]== 7 || dats[1]== 8 || dats[1]== 10 || dats[1]== 12) {
if (dats[0] > 31) {
alert("Invalid Date Format");
}

} else if (dats[0] > 30) {
alert("Invalid Date Format");


}
else {
return true;
}
}
}

</script>
</body>
</html>




Answer

check for leap year with this

if(((dats[2] % 4 == 0) && (dats[2] % 100 != 0)) || (dats[2] % 400 == 0))

then check for month within the above if condition and have you logic... easy :)

Comments