<script type = "text/javascript" >
function DateCheck(obj, type) { // 날짜 체크
if (obj.value != "") {
var date = obj.value;
var temp1 = type == 0 ? auto_hyphen_from(date) : auto_hyphen_to(date);
obj.value = temp1;
}
}
function auto_hyphen_from(date) {
var RegNotNum = /[^0-9]/g;
var dataForm = "$1-$2-$3";
var RegDate = /([0-9]{4})([0-9]{2})([0-9]+)/;
var temp1 = date.replace(RegNotNum, "");
if (temp1.length < 8) {
var dateTime = new Date();
var chkYear = dateTime.getFullYear();
var chkMonth = dateTime.getMonth();
var chkDate = dateTime.getDate();
var newDate = chkYear + "-" + (chkMonth.toString().length < 2 ? ("0" + chkMonth) : chkMonth) + "-" + chkDate;
return auto_hyphen_from(newDate);
} else if (temp1.length > 8) {
var date = temp1.substring(0, 8);
return auto_hyphen_from(date);
}
while (RegDate.test(temp1)) {
temp1 = temp1.replace(RegDate, dataForm);
}
if (isValidDate(temp1)) {
return temp1;
} else {
var dateTime = new Date();
var chkYear = dateTime.getFullYear();
var chkMonth = dateTime.getMonth();
var chkDate = dateTime.getDate();
var newDate = chkYear + "-" + (chkMonth.toString().length < 2 ? ("0" + chkMonth) : chkMonth) + "-" + chkDate;
return auto_hyphen_to(newDate);
}
}
function auto_hyphen_to(date) {
var RegNotNum = /[^0-9]/g;
var dataForm = "$1-$2-$3";
var RegDate = /([0-9]{4})([0-9]{2})([0-9]+)/;
var temp1 = date.replace(RegNotNum, "");
if (temp1.length < 8) {
var dateTime = new Date();
var chkYear = dateTime.getFullYear();
var chkMonth = dateTime.getMonth() + 1;
var chkDate = dateTime.getDate();
var newDate = chkYear + "-" + (chkMonth.toString().length < 2 ? ("0" + chkMonth) : chkMonth) + "-" + chkDate;
return auto_hyphen_to(newDate);
} else if (temp1.length > 8) {
var date = temp1.substring(0, 8);
return auto_hyphen_to(date);
}
while (RegDate.test(temp1)) {
temp1 = temp1.replace(RegDate, dataForm);
}
if (isValidDate(temp1)) {
return temp1;
} else {
var dateTime = new Date();
var chkYear = dateTime.getFullYear();
var chkMonth = dateTime.getMonth() + 1;
var chkDate = dateTime.getDate();
var newDate = chkYear + "-" + (chkMonth.toString().length < 2 ? ("0" + chkMonth) : chkMonth) + "-" + chkDate;
return auto_hyphen_to(newDate);
}
}
function isValidDate(param) {
try {
param = param.replace(/-/g, '');
// 자리수가 맞지않을때
if (isNaN(param) || param.length != 8) {
return false;
}
var year = Number(param.substring(0, 4));
var month = Number(param.substring(4, 6));
var day = Number(param.substring(6, 8));
var dd = day / 0;
if (month < 1 || month > 12) {
return false;
}
var maxDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var maxDay = maxDaysInMonth[month - 1];
// 윤년 체크
if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) {
maxDay = 29;
}
if (day <= 0 || day > maxDay) {
return false;
}
return true;
} catch (err) {
return false;
}
}
<
/script>