Questo codice che non funziona, l'ho provato a scrivere. Spero che vedendolo qualcuno posso aiutarmi a correggerlo:
codice:
<script language="javascript">
// <!CDATA[
function Button1_onclick(){
var s_data = document.getElementById("datanascita").value;
var x_data = document.getElementById("datanascitar").value;
if ((document.step2.cap.value)!=""){
if (!/[0-9]+/.test(document.step2.cap.value)){
alert("Il campo 'Cap' deve contenere solo numeri!");
document.step2.cap.focus();
return false;}
}
if ((document.step2.telefono.value)!=""){
if (!/[0-9]+/.test(document.step2.telefono.value)){
alert("Il campo 'Telefono' deve contenere solo numeri!");
document.step2.telefono.focus();
return false;}
}
if ((document.step2.partiva.value)!=""){
if (!/[0-9]+/.test(document.step2.partiva.value)){
alert("Il campo 'Partita Iva' deve contenere solo numeri!");
document.step2.partiva.focus();
return false;}
}
if ((document.step2.capsede.value)!=""){
if (!/[0-9]+/.test(document.step2.capsede.value)){
alert("Il campo 'Cap' deve contenere solo numeri!");
document.step2.capsede.focus();
return false;}
}
if ((document.step2.telefonosede.value)!=""){
if (!/[0-9]+/.test(document.step2.telefonosede.value)){
alert("Il campo 'Telefono' deve contenere solo numeri!");
document.step2.telefonosede.focus();
return false;}
}
if ((document.step2.capr.value)!=""){
if (!/[0-9]+/.test(document.step2.capr.value)){
alert("Il campo 'Cap' deve contenere solo numeri!");
document.step2.capr.focus();
return false;}
}
if ((document.step2.telefonor.value)!=""){
if (!/[0-9]+/.test(document.step2.telefonor.value)){
alert("Il campo 'Telefono' deve contenere solo numeri!");
document.step2.telefonor.focus();
return false;}
}
if(IsDate(s_data))
{
a_data = s_data.split(/\//g);
if(parseInt(a_data[2]) <= 99)
{
alert("L'anno della data deve essere di 4 cifre");
document.step2.datanascita.focus();
return false;
}
var d_data = new Date(a_data[2], a_data[1]-1, a_data[0]);
var d_oggi = new Date(); d_oggi = new Date(d_oggi.getFullYear(), d_oggi.getMonth(), d_oggi.getDate());
if(d_data > d_oggi){
alert("La data è maggiore della odierna");
document.step2.datanascita.focus();
return false;}
}
else if(IsDate(x_data)) {
a_data = x_data.split(/\//g);
if(parseInt(a_data[2]) <= 99)
{
alert("L'anno della data deve essere di 4 cifre");
document.step2.datanascitar.focus();
return false;
}
var d_data = new Date(a_data[2], a_data[1]-1, a_data[0]);
var d_oggi = new Date(); d_oggi = new Date(d_oggi.getFullYear(), d_oggi.getMonth(), d_oggi.getDate());
if(d_data > d_oggi){
alert("La data è maggiore della odierna");
document.step2.datanascitar.focus();
return false;}
}
else { // se non e' una data valida, ritorno false
return false;
}
return true; // se passa tutti i controlli, per pulizia, ritorno true
}
//------------------------------------------------
//valida la stringa dateStr
//------------------------------------------------
function IsDate(dateStr)
{
var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{4})$/;
var matchArray = dateStr.match(datePat);
if ((document.step2.datanascita.value)!=""){
if (matchArray == null){
alert("Digitare la data nel formato dd/mm/yyyy");
return false;}
day = matchArray[1];
month = matchArray[3];
year = matchArray[5];
if (month < 1 || month > 12){
alert("I mesi dell'anno sono 12!");
return false;}
if (day < 1 || day > 31){
alert("I giorni del mese vanno dall' 1 al 31!");
return false;}
if ((month==4 || month==6 || month==9 || month==11) && day==31){
alert("Il mese "+month+" non ha 31 giorni!");
return false;}
if (month == 2){
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)){
alert("Febbraio " + year + " non ha " + day + " giorni!");
return false;}
}
}
else if ((document.step2.datanascitar.value)!=""){
if (matchArray == null){
alert("Digitare la data nel formato dd/mm/yyyy");
return false;}
day = matchArray[1];
month = matchArray[3];
year = matchArray[5];
if (month < 1 || month > 12){
alert("I mesi dell'anno sono 12!");
return false;}
if (day < 1 || day > 31){
alert("I giorni del mese vanno dall' 1 al 31!");
return false;}
if ((month==4 || month==6 || month==9 || month==11) && day==31){
alert("Il mese "+month+" non ha 31 giorni!");
return false;}
if (month == 2){
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)){
alert("Febbraio " + year + " non ha " + day + " giorni!");
return false;}
}
}
}
return true;}
// ]]>
</script>