Poco tempo fa ho postato questo codice. Fa il controllo a priori e quindi non occorre più controllare i dati.
codice:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
//----------------------------------------------------
// variabili globali
//----------------------------------------------------
var globalYear = 0;
var globalmonth = 0;
var globalDay = 0;
var mese = new Array('Gen','Feb','Mar','Apr','Mag','Giu','Lug','Ago','Set','Ott','Nov','Dic');
//----------------------------------------------------
// Gestione degli anni
//----------------------------------------------------
function years(aYear) {
if (aYear == null) {
var today = new Date();
currentYear = today.getFullYear();
//------------ Assegnazione alla variabile globale
globalYear = currentYear;
} else {
currentYear = aYear;
//------------ Assegnazione alla variabile globale
globalYear = aYear;
//------------ Inizializzazione mesi
months(1);
return;
} // if (aYear == null)
//---------- Fino a 50 anni prima dell'anno in corso
startYear = currentYear - 50;
//---------- Fino a 5 anni dopo dell'anno in corso
endYear = currentYear + 5;
//---------- Riempimento del combo box "anno"
for (i=startYear;i<=endYear;i++) {
//----------- creazione di una nuova option
var newOption = document.createElement('OPTION');
//----------- Assegnazione della nuova opzione al combo
document.getElementById('anno').add(newOption);
//----------- Assegnazione del valore e del testo
newOption.text = i;
newOption.value = i;
//----------- Se l'anno in corso = a quello corrente
if (i == currentYear) {
newOption.selected = true;
} // if (i == currentYear)
} // for (i=startYear;i<=endYear;i++)
if (aYear != null) {
//------------ Inizializzazione mesi
months(1);
return;
} // if (aYear != null)
} // function years(aYear)
//----------------------------------------------------
// Gestione dei mesi
//----------------------------------------------------
function months(aMonth) {
if (aMonth == null) {
//------------ Mese in corso
var today = new Date();
//------------ Ricavare il mese in corso + 1 perché Gennaio = 0
currentMonth = today.getMonth() + 1;
//------------ Assegnazione alla variabile globale
globalMonth = currentMonth;
} else {
currentMonth = aMonth;
//------------ Assegnazione alla variabile globale
globalMonth = aMonth;
} // if (aMonth == null)
//------------ Cancellazione dei dati già presenti
document.getElementById('mese').innerHTML = '';
//------------ In ogni anno ci sono sempre 12 mesi
for (i=1;i<=12;i++) {
//----------- creazione di una nuova option
var newOption = document.createElement('OPTION');
//----------- Assegnazione della nuova opzione al combo
document.getElementById('mese').add(newOption);
//----------- Assegnazione del valore e del testo
newOption.text = mese[i-1];
newOption.value = i;
//----------- Se il mese in corso = a quello corrente
if (i == currentMonth) {
newOption.selected = true;
} // if (i == currentMonth)
} // for (i=1;i<=12;i++)
if (aMonth != null) {
//------------ Inizializzazione giorni
days(1);
} // if (aMonth != null)
} // function months()
//----------------------------------------------------
// Gestione dei giorni
//----------------------------------------------------
function days(aDay) {
//------------ Giorno in corso
if (aDay == null) {
var today = new Date();
//------------ Ricavare il mese in corso + 1 perché Gennaio = 0
currentDay = today.getDate();
} else {
currentDay = aDay;
} // if (aDay == null)
//------------ Cancellazione dei dati già presenti
document.getElementById('giorno').innerHTML = '';
//------------ In ogni mese ci sono almeno 28 giorni
for (i=1;i<=28;i++) {
//----------- creazione di una nuova option
var newOption = document.createElement('OPTION');
//----------- Assegnazione della nuova opzione al combo
document.getElementById('giorno').add(newOption);
//----------- Assegnazione del valore e del testo
newOption.text = i;
newOption.value = i;
//----------- Se il mese in corso = a quello corrente
if (i == currentDay) {
newOption.selected = true;
} // if (i == currentDay)
} // for (i=1;i<=28;i++)
//------------ Il mese di febbraio puo' avere 29 giorni
if (globalMonth == 2) {
//------------ Se è un anno bissestile - ATTENTO ci sono altri requisiti !!!!
if ((globalYear/4) == parseInt(globalYear/4)) {
//----------- creazione di una nuova option
var newOption = document.createElement('OPTION');
//----------- Assegnazione della nuova opzione al combo
document.getElementById('giorno').add(newOption);
//----------- Assegnazione del valore e del testo
newOption.text = 29;
newOption.value = 29;
//----------- Se il mese in corso = a quello corrente
if (currentDay == 29) {
newOption.selected = true;
} // if (currentDay == 29)
} // if ((globalYear/4) == parseInt(globalYear/4))
} else {
//----------- Tutti i mesi hanno almeno 30 giorni
//----------- creazione di una nuova option
var newOption = document.createElement('OPTION');
//----------- Assegnazione della nuova opzione al combo per il giorno 29
document.getElementById('giorno').add(newOption);
//----------- Assegnazione del valore e del testo
newOption.text = 29;
newOption.value = 29;
//----------- Se il mese in corso = a quello corrente
if (currentDay == 29) {
newOption.selected = true;
} // if (currentDay == 29)
//----------- creazione di una nuova option
var newOption = document.createElement('OPTION');
//----------- Assegnazione della nuova opzione al combo il giorno 30
document.getElementById('giorno').add(newOption);
//----------- Assegnazione del valore e del testo
newOption.text = 30;
newOption.value = 30;
//----------- Se il mese in corso = a quello corrente
if (currentDay == 30) {
newOption.selected = true;
} // if (currentDay == 30)
//----------- Alcuni mesi hanno 31 giorni
if (globalMonth != 4 && globalMonth != 6 && globalMonth != 9 && globalMonth != 11) {
//----------- creazione di una nuova option
var newOption = document.createElement('OPTION');
//----------- Assegnazione della nuova opzione al combo il giorno 31
document.getElementById('giorno').add(newOption);
//----------- Assegnazione del valore e del testo
newOption.text = 31;
newOption.value = 31;
//----------- Se il mese in corso = a quello corrente
if (currentDay == 31) {
newOption.selected = true;
} // if (currentDay == 30)
} // if (globalMonth != 4 && globalMonth != 6 && globalMonth != 9 && globalMonth != 11)
} // if (globalMonth == 2)
//------------ Assegnazione alla variabile globale
globalDay = currentDay;
} // function days()
//-->
</script>
</head>
<body>
Giorno <select id="giorno" ></select>
Mese <select id="mese" onchange="months(this.value);"></select>
Anno <select id="anno" onchange="years(this.value);"></select>
<script language="JavaScript" type="text/javascript">
<!--
years();
months();
days();
//-->
</script>
</body>
</html>