ここで、http: //codebins.com/bin/4ldqpah で上記の問題の完全なビンを作成しました
HTML:
<div id="panel">
<form id="frm1" method="post">
<input type="hidden" id="h_date" name="h_date" />
Enter Date:
<input type="text" name="txtdate" id="txtdate" onblur="setExpDate(this.value);" size="15"/>
<div id="hiddendiv">
</div>
</form>
</div>
CSS:
#panel form{
border:1px solid #454545;
padding:10px;
width:350px;
background:#a1a6a4;
}
#panel input{
border:1px solid #456699;
}
#panel input:focus{
background:#afffff;
}
Javascript:
function setExpDate(strdate) {
if (strdate != "" && strdate != null && typeof(strdate) != "undefined") {
if (isValidDate(strdate)) {
var interval = 30;
var userdate = new Date(strdate);
var newdate = new Date(userdate.getTime() - 30 * 24 * 60 * 60 * 1000);
document.getElementById("h_date").value = (newdate.getMonth() + 1) + "/" + newdate.getDate() + "/" + newdate.getFullYear();
document.getElementById("hiddendiv").innerHTML = "Set Hidden Date:" + document.getElementById("h_date").value + " (Before 30 days)";
} else {
alert("Please enter valid date.");
}
}
}
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YYYY
// Also separates date into month, day, and year variables
var datePat = /^(\d{2,2})(\/)(\d{2,2})\2(\d{4}|\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date must be in MM/DD/YYYY format")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31");
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
alert("Month " + month + " doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day == 29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true; // date is valid
}
http://codebins.com/bin/4ldqpahで解決策を試してください