I have a text field like that:
<input type=text name=date >
In this text field the user can enter the date in dd/mm/yyyy format.
but the future date is not allowed, except the past and present date is allowed.
thanx
abhijit
| SitePoint Sponsor |
I have a text field like that:
<input type=text name=date >
In this text field the user can enter the date in dd/mm/yyyy format.
but the future date is not allowed, except the past and present date is allowed.
thanx
abhijit





1)Check to see if it is a valid date according to your format. Search the web for the hundreds of regular expressions for validating a date. You can start here: http://www.regexlib.com/DisplayPatte...5&categoryId=5
2) If it is a valid date according to your format, use split('/') to split the string into days, months and years.
var date_array=dateEntered.split('/');
3) Create a date object for the entered date:
var enteredD = new Date(date_array[2], date_array[1], date_array[0]).
4) Create a date object for the current date:
var currentD = new Date();
5) Subtract the date objects:
var difference= currentD - enteredD;
6) If the difference is positive, then the date is valid. If the difference is negative, the entered date is in the future.
7) You have to figure out how to deal with dates before 1970 because as far as I know the Date object can only handle comparing dates after 1970.
An alternate method would be to compare the entered year stored in date_array[2] to the current year. You can get the current year from currentD like this:
var year = currentD.getFullYear();
If the entered year is more than the current year then the date is invalid; if it is less it is valid. If the years are the same, then you have to check the months:
var month=currentD.getMonth();
Months are indexed from 0 to 11, so you need to add 1 to that result to compare it to the entered month. If the entered month is greater than the current month, then the date is invalid; if it is less, then the date is valid. If it is the same, you have to check the days.
The days of the month are called the "date" in javascript:
var days = currentD.getDate();
The days are indexed normally from 1-31.
I hope that helps.
Why not use a calendar widget? They're- and many are free.
http://www.dynarch.com/projects/calendar/
::: certified wild guess :::
...and users won't screw up date formattingOriginally Posted by adios
![]()
Bookmarks