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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date in form ddmmyyyy needs simple valid/invalid test

Status
Not open for further replies.

gsamm

Programmer
Jun 2, 2005
13
0
0
GB
I have three listboxes for a date - day,month and year.

The day listbox has values 1-31
The Month listbox has values 1-12
The Year listbox has values 2005-2010

I concatonate these values and pass them to a database, so I have control over the format.

This works fine until you select 31/06/2005 (an invalid date because June only has 30 days)

I need a simple javascript test to tell me whether a date is valid or invalid.

I have a rudementary knoweledge of javascript and could program a solution but I would like to avoid a lengthy coding process if there is a simple answer.

I have found several lengthy date validation routines on the web, but they mainly deal with dates that are of an indeterminate format.
 
gsamm, check the FAQS link directly above and read the section on dynamic listboxes, then you can have your pulldowns change accordingly as the dates are selected so that an invalid date will never occur.

If this isn't an option then you can just do a simple if statement to check for invalid selections in the onchange handler of the pulldown, or the onsubmit handler of the form.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
This is what I have ended up with...

function MM_Validate()
{
strError = "There appears to be a problem with some of the data you input:\n\n"

var yearToCheck = document.formproject01.completedateyy.value;
var monthToCheck = document.formproject01.completedatemm.value - 1;
var dateToCheck = document.formproject01.completedatedd.value;
var dateObj = new Date(yearToCheck, monthToCheck, dateToCheck);

if (dateObj.getFullYear() !== yearToCheck && dateObj.getMonth() !== monthToCheck && dateObj.getDate() !== dateToCheck)
{
strError = strError + '- Project deadline is not a valid date.\n\n';
}

if(strError != '')
{
alert(strError)
return false;
}
else
{
return true;
}
}

I'll modify this to accept the fieldnames as parameters. It appears to work and gives me the yes/no answer I need. If any javascript guru spot a flaw in it I would appreciate it though :eek:)
 
I choose a different approach... in a jsp web app i use a pop-up javascript calendar.. so the user simply can not insert an INVALID date
 
I dunno why this thread got dug up after 3 months, but if you really wanna read the pros/cons against text validation and built in javascript objects read here:

thread216-1042923

or for another good clivec read:
thread215-1049618

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top