Hi,
I have a website where a user must enter a date that is not in the past, ie today or greater, and I want to use validation to ensure that the user entered data is not in the past. How do I set the minvalue to today's date dynamically so that I don't have to change the minvalue every day at midnight?![]()
Thanks in advance,
Joshua
FYI, I'm using DW CS 5.5 and Spry 1.6.1, and the Spry validation textfeild with the 'date' type selected to gather the user input.
Try
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet">
</head>
<body>
<p id="spryDate">
<label for="chosenDate">Date:</label>
<input type="text" name="chosenDate" id="chosenDate">
<span class="textfieldRequiredMsg">A Date is Required.</span>
<span class="textfieldInvalidFormatMsg">Incorrect Date.</span>
</p>
<script src="SpryAssets/SpryValidationTextField.js"></script>
<script>
var spryDate = new Spry.Widget.ValidationTextField("spryDate", "custom", {validation:checkdate, validateOn:["blur"]});
function checkdate(value) {
var entry = value.split('/');//splits input value;
var y = entry[2]*1;
var m = entry[1]*1-1;//sets months to default values i.e Jan = 0; Feb = 1; etc.
var d = entry[0]*1;
var newDate = new Date(y,m,d); //sets the chosen date
var today = new Date();
dif = newDate-today;//difference in milliseconds
dif = Math.ceil(dif/1000/60/60/24);//difference in days
if(dif >= 0){//if the date is not in the past, test if date is valid
return /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/.test(value);
} else {
return false;
}
}
</script>
</body>
</html>
Gramps
You may have to adapt to your locale (i.e., date format), but Spry does check against a minimum date:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<span id="sprytextfield1">
<label for="txtDate">Date:</label>
<input type="text" name="txtDate" id="txtDate" />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldMinValueMsg">The entered value is less than the minimum required.</span></span>
<input type="submit" name="button" id="button" value="Submit" />
</form>
<script type="text/javascript">
var today = new Date();
var minDate = ('0' + (today.getMonth() + 1)).substr(-2,2) + '/' + ('0' + today.getDate()).substr(-2, 2) + '/' + today.getFullYear();
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "date", {validateOn:["blur"], minValue:minDate, hint:"mm/dd/yyyy", format:"mm/dd/yyyy"});
</script>
</body>
</html>
Xavier
This worked, thanks for the help Xavier.
Gramps, for some reason, when I tried out your solution it doubled my input. ie, when i pressed the keys 1,2,3 the textbox output '11/22/33'. I'm not sure if it was an error in my implementation or an error in the solution.
Thanks again,
Joshua
North America
Europe, Middle East and Africa
Asia Pacific