View Full Version : validate date
ted_williams
11-08-2002, 03:38 PM
I need to validate a date (newdate less than todaydate) before the form is submitted (if its not true, I'll pop up a fancy alert( "Please enter a valid date" );
return false;.
I think its the syntax on the date handling thats kicking my butt.
I'm sure somehere can spit this out automacticaly, instead of me putting semi-colons all over the place and pulling my hair out.
Don't get me wrong, I'm going to stop working on it.
kdjoergensen
11-08-2002, 05:15 PM
Maybe you want to post your code ??
You could do this:
if (todaysYear > testDateYear){
showAlert("Year"); return false;
} elseif (todaysMonth > testDateMonth){
showAlert("Month"); return false;
} elseif (todaysDate > testDateDate){
showAlert("Date"); return false;
}
function showAlert(message){
var txt = "You have to enter a future date";
txt += "The "+message+" you entered is before ";
txt += "current"+message;
txt += "\r\n Please enter a proper date";
alert(txt);
}
It simply requires you to capture the date:month:year in seperate values, both for todays date and the date you are receiving as input from the user...
the 'elseif' statement ensures that it works correctly.
rparmar
11-08-2002, 06:23 PM
You can use VBSCRIPT function :-
DateDiff() as below example;
The following example uses the DateDiff function to display the number of days between a given date and today:
Function DiffADate(theDate)
DiffADate = "Days from today:" & DateDiff("d",Now,theDate)
End Function
RAJ
rparmar
11-08-2002, 06:26 PM
To validate a date, use Isdate() function
again from VBScript function as below :-
The following example uses the IsDate function to determine whether an expression can be converted to a date:
Dim MyDate, YourDate, NoDate, MyCheck
MyDate = "October 19, 1962": YourDate = #10/19/62#: NoDate = "Hello"
MyCheck = IsDate(MyDate) ' Returns True.
MyCheck = IsDate(YourDate) ' Returns True.
MyCheck = IsDate(NoDate) ' Returns False.
There are others to validate other types IsNull(),IsEmpty(), etc...
RAJ
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.