View Full Version : date validation

11-10-2004, 07:14 PM

i want to validate a date in the format mm/dd/yyyy.i want users to enter 2 digit month and 2 digit date and 4 digit year.

i am doing some validation the date should be 1st or 15 th of month it's working fine.i want to alert if they don't enter in this format mm/dd/yyyy.i want to alert a separate message box if date pattern not matches.

my code is

var xVal=window.document.frmNew.txtDate.value;
var xDate = new Date(xVal);
var today = new Date();
if (xDate > today && (xDate.getDate()==15 || xDate.getDate()==01))
return true;
alert("The effective date should be the 1st or 15th of the month and be greater than current date.");
return false;

11-10-2004, 09:31 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<form onsubmit="return validate()">
<label for="year">Year</label>
<input id="year" type="text" size="4" maxlength="4">

<label for="month">Month</label>
<select id="month">
<option value="00">January</option>
<option value="01">February</option>
<option value="02">March</option>
<option value="03">April</option>
<option value="04">May</option>
<option value="05">June</option>
<option value="06">July</option>
<option value="07">August</option>
<option value="08">September</option>
<option value="09">October</option>
<option value="10">November</option>
<option value="11">December</option>

<label for="date">Date</label>
<select id="date">
<option value="01">01</option>
<option value="15">15</option>

<input type="submit">
<script type="text/javascript">
function validate()
var now = new Date();
var sel = new Date(document.getElementById("year").value, document.getElementById("month").value, document.getElementById("date").value);

if(sel < now)
return false;

return true;