Monday, April 20, 2009

how to validate textbox value (Date Format)

<html>
<head>
<script language="javascript">
function checkDt(dtVal) {
var mo, day, yr;
var entry = dtVal.value;
var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
var valid = (reLong.test(entry)) || (reShort.test(entry));
if (valid) {
var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
var delim1 = entry.indexOf(delimChar);
var delim2 = entry.lastIndexOf(delimChar);
mo = parseInt(entry.substring(0, delim1), 10);
day = parseInt(entry.substring(delim1+1, delim2), 10);
yr = parseInt(entry.substring(delim2+1), 10);
// two-digit of year
if (yr < 100) {
var today = new Date();
// get floor of current century
var currCent = parseInt(today.getFullYear() / 100) * 100;
// two digits up to this year + 15 expands to current century
var threshold = (today.getFullYear() + 15) - currCent;
if (yr > threshold) {
yr += currCent - 100;
} else {
yr += currCent;
}
}
var testDate = new Date(yr, mo-1, day);
if (testDate.getDate() == day) {
if (testDate.getMonth() + 1 == mo) {
if (testDate.getFullYear() == yr) {
dtVal.value = mo + "/" + day + "/" + yr;
return true;
} else {
alert("Check the year entry.");
}
} else {
alert("Check the month entry.");
}
} else {
alert("Check the date entry.");
}
} else {
alert("Invalid date format. Enter as mm/dd/yyyy.");
}
return false;
}

</script>
</head>
<body>
<input type="text" id="txtDt" onblur="checkDt(this)" />
</body>



No comments:

Creative Commons License
Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.