Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

restrict date entry to date() - 90 1

Status
Not open for further replies.

deharris2003

Programmer
Jul 1, 2003
41
US
I am new to javascript. What I want to do is not allow the user to enter a date that is greater than 90 days in the past. I have written the following. doesn't seem to work. Can you tell me what I am doing wrong

<script type=&quot;javascript&quot;>
function verifyDate(field)
{
var today = date();
if field < today
{
alert(&quot;date must be within 90 days of today&quot;)
}
}
</script>
 
what is field ?

Known is handfull, Unknown is worldfull
 
firstly dont keep it as a textfield as the user can enter any data, keep it as a dropwdown menu (MM/DD/YY).
is this suggestion o.k?

Known is handfull, Unknown is worldfull
 
Sure I can do that. But I would need help in sending the data to the function
 
u have to change ur function. are u hell bent on retaining ur old function?

Known is handfull, Unknown is worldfull
 
Well Not really cause it doesnt work. I am not really good at javascript just leaning it really.
 
then please rewrite it. call the funtion when the form is submitted. get the values of the select boxes and create a date out of it. its pretty simple. a search in this forum will yield better results...

Known is handfull, Unknown is worldfull
 
deharris2003, here mate, have a play around with this code. Should get you further down the track.

[tt]
<html>
<head>
<script>
function doValidateField(objField){
// Get today's date
var dateToday = new Date();
//get the date from the string entered in the field
var dateEntered = new Date(objField.value);
// Subtracting two dates gives us the number of milliseconds
// between them
var intDateDiff = dateToday - dateEntered;
// Divide this by 86400000 (milliseconds in a day)
// and floor it to round off any decimal
var intDaysDiff = Math.floor(intDateDiff / 86400000);
//check if the days difference is less than 0
// ie some time in the future:
if(intDaysDiff < 0){
//if it is, alert the user
alert('The date cannot be greater than today\'s date.\n' +
'Please enter a date no greater than 90 days prior to today');
}
//check if the days difference is greater than 90
// ie too far in the past:
if(intDaysDiff > 90){
//if it is, alert the user
alert('The date cannot be more than 90 days prior to today.\n' +
'Please enter a date no greater than 90 days prior to today');
}
}
</script>
</head>
<body>
<input type=&quot;text&quot;
name=&quot;fldDateEntered&quot;
id=&quot;fldDateEntered&quot;
onblur=&quot;doValidateField(this);&quot;
value=&quot;9/24/2003&quot;>
</body>
</html>[/tt]

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top