Check for valid date using JavaScript. below is the full code.
  1. <head id="Head1" runat="server">
  2. <title>Untitled Page</title>
  3. <script language="javascript" type="text/javascript">
  4. // Declaring valid date character, minimum year and maximum year
  5. var dtCh = "/";
  6. var minYear = 1900;
  7. var maxYear = 2100;
  8. function isInteger(s) {
  9. var i;
  10. for (i = 0; i < s.length; i++) {
  11. // Check that current character is number.
  12. var c = s.charAt(i);
  13. if (((c < "0") || (c > "9"))) return false;
  14. }
  15. // All characters are numbers.
  16. return true;
  17. }
  18. function stripCharsInBag(s, bag) {
  19. var i;
  20. var returnString = "";
  21. // Search through string's characters one by one.
  22. // If character is not in bag, append to returnString.
  23. for (i = 0; i < s.length; i++) {
  24. var c = s.charAt(i);
  25. if (bag.indexOf(c) == -1) returnString += c;
  26. }
  27. return returnString;
  28. }
  29. function daysInFebruary(year) {
  30. // February has 29 days in any year evenly divisible by four,
  31. // EXCEPT for centurial years which are not also divisible by 400.
  32. return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
  33. }
  34. function DaysArray(n) {
  35. for (var i = 1; i <= n; i++) {
  36. this[i] = 31
  37. if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
  38. if (i == 2) { this[i] = 29 }
  39. }
  40. return this
  41. }
  42. function isDate(dtStr) {
  43. var daysInMonth = DaysArray(12)
  44. var pos1 = dtStr.indexOf(dtCh)
  45. var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
  46. var strMonth = dtStr.substring(0, pos1)
  47. var strDay = dtStr.substring(pos1 + 1, pos2)
  48. var strYear = dtStr.substring(pos2 + 1)
  49. strYr = strYear
  50. if (strDay.charAt(0) == "0" && strDay.length > 1)
  51. strDaystrDay = strDay.substring(1)
  52. if (strMonth.charAt(0) == "0" && strMonth.length > 1)
  53. strMonthstrMonth = strMonth.substring(1)
  54. for (var i = 1; i <= 3; i++) {
  55. if (strYr.charAt(0) == "0" && strYr.length > 1)
  56. strYrstrYr = strYr.substring(1)
  57. }
  58. month = parseInt(strMonth)
  59. day = parseInt(strDay)
  60. year = parseInt(strYr)
  61. if (pos1 == -1 || pos2 == -1) {
  62. alert("The date format should be : mm/dd/yyyy")
  63. return false
  64. }
  65. if (strMonth.length < 1 || month < 1 || month > 12) {
  66. alert("Please enter a valid month")
  67. return false
  68. }
  69. if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
  70. alert("Please enter a valid day")
  71. return false
  72. }
  73. if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
  74. alert("Please enter a valid 4 digit year between " + minYear + " and " + maxYear)
  75. return false
  76. }
  77. if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
  78. alert("Please enter a valid date")
  79. return false
  80. }
  81. return true
  82. }
  83. function ValidateForm() {
  84. var dt;
  85. dt = document.getElementById('txtDate');
  86. if (isDate(dt.value) == false) {
  87. dt.focus()
  88. return false
  89. }
  90. return true
  91. }
  92. </script>
  93. </head>
  94. <body>
  95. <form id="form2" runat="server">
  96. <div>
  97. <asp:TextBox ID="txtDate" runat="server">
  98. </asp:TextBox>
  99. <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ValidateForm()" />
  100. </div>
  101. </form>
  102. </body>