Thursday, January 7, 2010

Validating Date Using JavaScript.

Date Should not be Character.
-------------------------------

function IsChar(sText) {
var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. ";
var IsNumber = true;
var Char;


for (i = 0; i < sText.length && IsNumber == true; i++) { Char = sText.charAt(i); if (ValidChars.indexOf(Char) == -1) { IsNumber = false; } } return IsNumber; } Date Should Not Contains Special Characters. ---------------------------------------- function checkSpecialchar(objName) { var checkOK = "!@#$%^&*()_-+|\<>?.";
var checkStr = objName;

var allValid = true;
var allNum = "";
var ch;

for (i = 0; i < checkStr.length; i++) { ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break; } //if (ch != ",") //allNum += ch; } if (!allValid) { return (false); } else { return (true); } } Now we Start validating date. Here is the code for that. ------------------------------------------------ var startDate = document.forms[0].txtStartDate.value; var validDate = startDate.indexOf('/'); var fIndex = startDate.charAt(2); var lIndex = startDate.charAt(5); if (fIndex != '/') { alert("Please Use (Slash) /to Enter Date"); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } if (lIndex != '/') { alert("Please Use (Slash) /to Enter Date"); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } if (validDate == -1) { alert("Please Use (Slash) /to Enter Date"); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } else if (CheckSpecialChar(startDate)) { alert("Special Characters are not allowed."); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } else if (IsChar(startDate)) { alert("Date Should be Numeric."); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } else { var txtDate = startDate.split('/'); var day = txtDate[0]; var month = txtDate[1]; var year = txtDate[2]; var dt = new Date(); var curYear = dt.getFullYear(); var curMonth = dt.getMonth() + 1; if (year < curYear) { alert("Year Should be Greater Than or Equal to Current Year."); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } if (year == curYear) { if (month < curMonth) { alert("Month Should be greater than current month."); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } } if (month > 12) {
alert("Please Enter date in DD/MM/YYYY Format.");
document.forms[0].txtStartDate.focus();
document.forms[0].txtStartDate.select();
return false;
}
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day > 31) {
alert("Day Should be less than or equal to 31 for the given month.");
document.forms[0].txtStartDate.focus();
document.forms[0].txtStartDate.select();
return false;
}
}
if (month == 2 && (year % 4 == 0 || year % 400 == 0)) {
if (day > 29) {
alert("Leap Year February day should be less than or equal to 29");
document.forms[0].txtStartDate.focus();
document.forms[0].txtStartDate.select();
return false;
}
}
if (month == 2 && (year % 4 != 0 || year % 400 != 0)) {
if (day > 28) {
alert("Not a Leap Year February day should be less than or equal to 28");
document.forms[0].txtStartDate.focus();
document.forms[0].txtStartDate.select();
return false;
}
}

=============================================================
Enjoy....

No comments:

Post a Comment