Forse il codice è un po' ampolloso, ma lo fatto per prendere pratica con gli oggetti. Vedi se può servire
codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Pagina senza titolo</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
function Button1_onclick(v)
{
var form = v.form;
var ret = verifica();
if(ret.isvalid) form.submit(); else alert(ret.message);
}
function verifica()
{
var ret = new Object();
ret.message = "";
ret.isvalid = true;
var t1 = document.getElementById("Text1");
var t2 = document.getElementById("Text2");
var v1 = trim(t1.value);
if( (v1 != "" && !isdate(v1).isdate) )
{
ret.isvalid = false;
ret.message += "&La data deve essere nel formato gg-mm-aaaa";
}
if(trim(t2.value) == "")
{
ret.isvalid = false;
ret.message += "&Il campo Text2 non deve essere vuoto";
}
ret.message = ret.message.substr(1);
return ret;
}
/* funzioni di libreria-------------------------------------------------------*/
/*---------------------------------------------------------------------
Questa funzione accetta una variabile stringa e verifica se e una data.
La data deve essere nel formato giorno mese anno. Il giorno e il mese
devono essere di 1 o 2 cifre, l'anno deve essere di 2 o 4 cifre.
Il delimitatore deve essere / o - o .
Restituisce un oggetto con le proprieta:
.isdate = vero o falso
.error = messaggio errore
.year = anno
.month = mese (1 - 12)
.day = giorno (1 - 31)
.data = oggetto data
---------------------------------------------------------------------*/
function isdate(dateStr)
{
var ret = new Object();
ret.isdate = false;
ret.error = "";
ret.year = 0;
ret.month = 0;
ret.day = 0;
ret.data = null;
// inizio stringa
// 1 o 2 cifre
// / o - o .
// 1 o 2 cifre
// / o - o .
// 4 cifre
// fine stringa
//var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{4})$/;
//var datePat = /^\s*(\d{1,2})(\/|-|\.)(\d{1,2})(\2)(\d{2}|\d{4})\s*$/;
/* modificata per il formato gg-mm-aaaa */
var datePat = /^\s*(\d{2})(-)(\d{2})(\2)(\d{4})\s*$/;
//restituisce, allo stesso modo di una matrice, i risultati di una ricerca
//di una stringa utilizzando un oggetto Regular Expression
var matchArray = dateStr.match(datePat);
if (matchArray == null) {
ret.error = "Per favore, introduci la data nei formati gg-mm-aaaa ";
return ret;
}
day = matchArray[1];
month = matchArray[3];
year = matchArray[5];
if(year.length == 2)
if(parseInt(year,10) < 30)
year = parseInt(year,10) + 2000;
else
year = parseInt(year,10) + 1900;
if (month < 1 || month > 12) { // il mese deve essere compreso tra 1 e 12
ret.error = "Il mese deve essere compreso tra 1 e 12.";
return ret;
}
if (day < 1 || day > 31) {//il giorno non puo essere < 1 e > 31
ret.error = "Il giorno deve essere compreso tra 1 e 31";
return ret;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
//i mesi aprile, giugno, settembre, novembre, hanno 30 giorni
ret.error = "Il mese " + month + " non ha 31 giorni!";
return ret;
}
if (month == 2) { // verifica se l'anno e bisestile: febbraio puo avere 29 giorni
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
ret.error = "Febbraio " + year + " non ha " + day + " giorni!";
return ret;
}
}
isDateError = "";
// se sono qui, la data e valida!
ret.isdate = true;
ret.error = "";
ret.year = year;
ret.month = month;
ret.day = day;
ret.data = new Date(year, (month - 1), day);
return ret;
}
//-------------------------------------------------------------------
// rimuove gli spazi iniziali e finali da strValore
//-------------------------------------------------------------------
function trim(stringa)
{
stringa = stringa + "";
return stringa.replace(/^ */,"").replace(/ *$/,"");
}
// ]]>
</script>
</head>
<body>
<form id="form1" action="a.htm">
<table>
<tr>
<td><label>Data nel formato gg-mm-aaaa oppure nullo: </label></td>
<td><input id="Text1" type="text" /></td>
</tr>
<tr>
<td><label>Testo non può essere nullo: </label></td>
<td><input id="Text2" type="text" /></td>
</tr>
</table>
<input id="Button1" type="button" value="Submit" onclick="return Button1_onclick(this)" />
</form>
</body>
</html>