Validate user-entered dates in Classic ASP

A form can collect year, month and day separately, validate them on the server and then build a Date value. Avoid constructing an ambiguous string such as yyyy/mm/dd and assuming every server locale will parse it the same way.

Complete Classic ASP example

<%@ Language=VBScript %>
<% Option Explicit %>
<%
Dim submitted, monthNo, dayNo, yearNo
Dim myDate, validDate

submitted = (Request.Form("todo") = "submit")

If submitted Then
  monthNo = Request.Form("month")
  dayNo = Request.Form("day")
  yearNo = Request.Form("year")
  validDate = False

  If IsNumeric(monthNo) And IsNumeric(dayNo) And IsNumeric(yearNo) Then
    monthNo = CInt(monthNo)
    dayNo = CInt(dayNo)
    yearNo = CInt(yearNo)

    If monthNo >= 1 And monthNo <= 12 And _
       dayNo >= 1 And dayNo <= 31 And _
       yearNo >= 100 And yearNo <= 9999 Then
      myDate = DateSerial(yearNo, monthNo, dayNo)
      validDate = (Year(myDate) = yearNo And Month(myDate) = monthNo And Day(myDate) = dayNo)
    End If
  End If

  If validDate Then
    Response.Write "<p>Valid date: " & Server.HTMLEncode(CStr(myDate)) & "</p>"
  Else
    Response.Write "<p>Please enter a valid date.</p>"
  End If
End If
%>
<form method="post" action="">
  <input type="hidden" name="todo" value="submit">
  <label>Month <select name="month" required>
  <% Dim m : For m = 1 To 12 %>
    <option value="<%=m%>"><%=MonthName(m)%></option>
  <% Next %>
  </select></label>
  <label>Day <select name="day" required>
  <% Dim d : For d = 1 To 31 %>
    <option value="<%=d%>"><%=d%></option>
  <% Next %>
  </select></label>
  <label>Year <input type="number" name="year" min="100" max="9999" value="2026" required></label>
  <button type="submit">Validate date</button>
</form>

The Year/Month/Day comparison after DateSerial() is important because DateSerial() normalizes overflow. For example, February 31 rolls into March instead of raising a validation error.

Server-side validation: HTML controls improve usability, but the ASP code must still validate every submitted value because client-side restrictions can be bypassed.

ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer