...

View Full Version : date validation



ugly
11-10-2004, 07:14 PM
hi

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.");
window.document.frmNew.txtDate.focus();
return false;
}

hemebond
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">
<head>
<title>47301</title>
</head>
<body>
<form onsubmit="return validate()">
<fieldset>
<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>
</select>

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

<input type="submit">
</fieldset>
</form>
</body>
<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;
}
</script>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum