var modificato; // -------- // la funzione controlla che venga inserito un orario nel formato // HH:MI e che questo orario sia valido. // -------- function chkhour(element) { var ora = 0; var minuti = 0; var filter=/^[0-9]{2}[:.,][0-9]{2}$/; inpHour = element.value; if (inpHour.length == 0 ) {return true;} if (inpHour.length < 5) { inpHour = "0"+inpHour; } if (!filter.test(inpHour)) { alert("Inserire ora nel formato HH:MI!"); element.focus(); element.select(); return false; } ora = inpHour.substr(0,2); minuti = inpHour.substr(3,2); if (( ora < 24 && minuti < 60 ) || ( ora == 24 && minuti == 0 )) { return true; } else //errore { alert("Orario non valido!"); element.focus(); element.select(); return false; } } // -------- // la funzione controlla che venga inserito un orario nel formato // HH:MI, sia valido e che l'ora specificata sia < 13, cioè della // mattina. // -------- function chkhour_am(element) { var ora = 0; var minuti = 0; var filter=/^[0-9]{2}[:.,][0-9]{2}$/; inpHour = element.value; if (inpHour.length == 0 ) {return true;} if (inpHour.length < 5) { inpHour = "0"+inpHour; } if (!filter.test(inpHour)) { alert("Inserire l'ora nel formato HH:MI!"); element.focus(); element.select(); return false; } ora = inpHour.substr(0,2); minuti = inpHour.substr(3,2); if (( ora < 24 && minuti < 60 ) || ( ora == 24 && minuti == 0 )) { if ( ora < 13 ) { return true; } else //errore { alert("Orario non valido per la mattina!"); element.focus(); element.select(); return false; } } else //errore { alert("Orario non valido!"); element.focus(); element.select(); return false; } } // -------- // la funzione controlla che venga inserito un orario nel formato // HH:MI, sia valido e che l'ora specificata sia > 12, cioè del // pomeriggio. // -------- function chkhour_pm(element) { var ora = 0; var minuti = 0; var filter=/^[0-9]{2}[:.,][0-9]{2}$/; inpHour = element.value; if (inpHour.length == 0 ) {return true;} if (inpHour.length < 5) { inpHour = "0"+inpHour; } if (!filter.test(inpHour)) { alert("Inserire l'ora nel formato HH:MI!"); element.focus(); element.select(); return false; } ora = inpHour.substr(0,2); minuti = inpHour.substr(3,2); if (( ora < 24 && minuti < 60 ) || ( ora == 24 && minuti == 0 )) { if ( ora >= 11 ) { return true; } else //errore { alert("Orario non valido per il pomeriggio!"); element.focus(); element.select(); return false; } } else //errore { alert("Orario non valido!"); element.focus(); element.select(); return false; } } // Controlla che entrambi i campi dell'orario siano presenti function Ctr_dallealle(giorno,dalle, alle) { if (dalle.value!='' && alle.value=='' ) { alert('Il campo < ALLE > di < ' + giorno + ' > deve essere impostato.'); alle.focus(); return false; } if (dalle.value=='' && alle.value!='' ) { alert('Il campo < DALLE > di < ' + giorno + ' > deve essere impostato.'); dalle.focus(); return false; } return true; } // Controlla che i campi dell'orario siano in successione function Ctr_orario(giorno,mdalle,malle,pdalle,palle) { var or1 = mdalle.value; var or2 = malle.value; var or3 = pdalle.value; var or4 = palle.value; or1 = or1.replace(/[,.]/, ":"); or2 = or2.replace(/[,.]/, ":"); or3 = or3.replace(/[,.]/, ":"); or4 = or4.replace(/[,.]/, ":"); if(!Ctr_dallealle(giorno+' mattina',mdalle,malle)) { return false;} if(!Ctr_dallealle(giorno+' pomeriggio',pdalle,palle)) { return false;} if ((or1.length == 0) && (or3.length == 0)) { return true; } if (or1.length == 0) { or1 = '00:00'; or2 = '00:01'; } else { if (or1.length == 4) { or1 = '0'+ or1; } if (or2.length == 4) { or2 = '0'+ or2; } } if (or3.length == 0) { or3 = '24:01'; or4 = '24:02'; } if (or1 >= or2) { alert('Gli orari della mattina di ' + giorno + ' non sono corretti.'); mdalle.focus(); return false; } if (or2 > or3) //accettabile coincidenza di o2 con o3 { alert('Sovrapposizione degli orari della mattina e del pomeriggio di ' + giorno + '.'); malle.focus(); return false; } if (or3 >= or4) { alert('Gli orari del pomeriggio di ' + giorno + ' non sono corretti.'); pdalle.focus(); return false; } return true; } //La funzione controlla l'obbligatorietà di un campo //Controlla che nel campo field ci sia un valore. function checkNull( field , fieldName) { selected = 0; fieldIsNull = 0; if ( field.type == "text" || field.type == "password" || field.type == "textarea" ) { if ( field.value == "" ) fieldIsNull = 1; } else if ( field.type == "select-one" ) { if ( field.selectedIndex <= 0) fieldIsNull = 1; } else if ( field.type == "select-multiple" ) { fieldIsNull = 1; for ( i = 0; i < field.length; i++ ) if ( field.options[i].selected ) fieldIsNull = 0; } else if ( field.type == "undefined" || field.type == "checkbox" || field.type == "radio" ) { fieldIsNull = 1; for ( i = 0; i < field.length; i++ ) if ( field[i].checked ) fieldIsNull = 0; } if ( fieldIsNull ) { if ( typeof fieldName == "undefined" ) alert( " Deve essere impostato." ); else alert("Il campo <" + fieldName + "> deve essere impostato." ); if ( field.type == "text" || field.type == "textarea" || field.type == "password" || field.type == "select-one" || field.type == "select-multiple" ) field.focus(); //field.select(); return false; } return true; } //Controlla che il CAP sia di 5 cifre function CtrlCap(Field, Label) { var ValidChar = "0123456789"; var OK = true; if (Field.value.length < 5){ alert("Il campo <" + Label + "> deve essere formato da 5 cifre.") Field.focus(); return false; } for (var i = 0; i < Field.value.length; i++) { var ch = Field.value.charAt(i) for (j = 0; j < ValidChar.length; j++) if (ch == ValidChar.charAt(j)) break; if (j == ValidChar.length) { OK = false; break; } } if (!OK) { alert("Il campo <" + Label + "> può contenere solo numeri!") Field.focus(); return false; } return true; } //Controlla che i valori siano tutte cifre function CtrlNum(Field, Label) { var ValidChar = "0123456789"; var OK = true; for (var i = 0; i < Field.value.length; i++) { var ch = Field.value.charAt(i) for (j = 0; j < ValidChar.length; j++) if (ch == ValidChar.charAt(j)) break; if (j == ValidChar.length) { OK = false; break; } } if (!OK) { alert("Il campo <" + Label + "> può contenere solo numeri!") Field.focus(); return false; } return true; } //Controlla la presenza di @ nella stringa function CtrlMail(Field, Label) { if(Field.value != "") { if (Field.value.indexOf('@', 0) == -1) { alert("Il campo <" + Label + "> non è un indirizzo di E-Mail valido!"); Field.focus(); return false; } } return true; } // La funzione controlla la lunghezza massima per un campo textarea // di 400 caratteri function lunghezza_400(Field) { var st = Field.value; if ( st.length > 400) { alert("Attenzione lunghezza massima 400 caratteri"); Field.focus(); Field.select(); return false; } return true; } // La funzione controlla la lunghezza massima per un campo textarea // di 200 caratteri function lunghezza_200(Field) { var st = Field.value; if ( st.length > 200) { alert("Attenzione lunghezza massima 200 caratteri"); Field.focus(); Field.select(); return false; } return true; } // La funzione controlla la lunghezza massima per un campo textarea // di 120 caratteri function lunghezza_120(Field) { var st = Field.value; if ( st.length > 120) { alert("Attenzione lunghezza massima 120 caratteri"); Field.focus(); Field.select(); return false; } return true; } // La funzione controlla la lunghezza massima per un campo textarea // di 240 caratteri function lunghezza_240(Field) { var st = Field.value; if ( st.length > 240) { alert("Attenzione lunghezza massima 240 caratteri"); Field.focus(); Field.select(); return false; } return true; } // La funzione controlla la lunghezza massima per un campo textarea // di 2000 caratteri function lunghezza_2000(Field) { var st = Field.value; if ( st.length > 2000) { alert("Attenzione lunghezza massima 2000 caratteri"); Field.focus(); Field.select(); return false; } return true; } // La funzione controlla la lunghezza massima per un campo textarea // di 500 caratteri function lunghezza_500(Field) { var st = Field.value; if ( st.length > 500) { alert("Attenzione lunghezza massima 500 caratteri"); Field.focus(); Field.select(); return false; } return true; } // funzione per ritrovare un oggetto mediante il nome indipendentemente dalla sua // posizione nella gerarchia della pagina function findObj(theObj, theDoc) { var p, i, foundObj; if(!theDoc) theDoc = document; if( (p = theObj.indexOf("?")) > 0 && parent.frames.length) { theDoc = parent.frames[theObj.substring(p+1)].document; theObj = theObj.substring(0,p); } if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj]; for (i=0; !foundObj && i < theDoc.forms.length; i++) foundObj = theDoc.forms[i][theObj]; for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) foundObj = findObj(theObj,theDoc.layers[i].document); if(!foundObj && document.getElementByName) foundObj = document.getElementByName(theObj); return foundObj; } // La funzione disabilita il campo hide1 se l'opzione selezionata nel campo // nameSelect supera il valore MaxOption function disabilita(nameSelect, maxOption, hide1) { var obj, obj2Hide1, max; obj = findObj(nameSelect); obj2Hide1 = findObj(hide1); max = maxOption; if ((obj.value)> max){ obj2Hide1.disabled=""; } else{ obj2Hide1.disabled="disabled"; } } // La funzione assegna l'unico valore presente, come default // (nameSelect e' una combobox) function setDefault(nameSelect) { var obj; obj = findObj(nameSelect); if (obj.length == 2) obj.selectedIndex=1; } // La funzione svuota e disabilita il campo field2 se l'opzione selezionata nel campo // field1 non e' uguale al valore RefValue function disabilita2(field1, RefValue, field2) { var f1, rval, f2; f1= findObj(field1); f2= findObj(field2); rval= RefValue; if ((f1.value)== rval){ f2.disabled=""; } else{ f2.value=""; f2.disabled="disabled"; } } // La funzione disabilita il campo field2 se l'opzione selezionata nel campo // field1 non e' uguale al valore RefValue (utile per i pulsanti) function disabilita3(field1, RefValue, field2) { var f1, rval, f2; f1= findObj(field1); f2= findObj(field2); rval= RefValue; if ((f1.value)== rval){ f2.disabled=""; } else{ f2.disabled="disabled"; } } // La funzione svuota e disabilita il campo field2 se l'opzione selezionata nel campo // field1 non e' uguale al valore RefValue function disabilitacampo(field1, RefValue1, RefValue2, field2) { var f1, rval, f2; f1= findObj(field1); f2= findObj(field2); rval1= RefValue1; rval2= RefValue2; if (((f1.value)== rval1) || ((f1.value)== rval2)){ f2.disabled=""; } else{ f2.value=""; f2.disabled="disabled"; } } //Verifica il formato di una data, nel senso che controlla: // - la correttezza del formato in termini gg-mm-aaaa // - la correzza dei mesi,giorni e anno //Ritorna true se la data è corretta e significativa, ritorna false altrimenti function verificaFormatoData(Field, Label) { if (Field.value == '') return true; // se la data è obbligatoria sarà chiamata prima la isNull e quindi qui il controllo non va fatto errore = 0; corretta =/^\d?\d[ /.-]\d?\d[ /.-]\d\d\d\d$/.test(Field.value); if (!corretta) { errore = 1; // errore sul formato della data } else { dataInp = Field.value; if (dataInp.charAt(1) == ' ' || dataInp.charAt(1) == '/' || dataInp.charAt(1) == '-' || dataInp.charAt(1) == '.') dataInp = '0' + dataInp if (dataInp.charAt(4) == ' ' || dataInp.charAt(4) == '/' || dataInp.charAt(4) == '-' || dataInp.charAt(4) == '.') dataInp = dataInp.substring(0,3) + '0' + dataInp.substring(3,9) if (dataInp.charAt(2) != dataInp.charAt(5)) errore = 2 else { giorno = dataInp.substring(0, 2) mese = dataInp.substring(3, 5) anno = dataInp.substring(6, 10) if (mese > 0 && mese < 13 && giorno > 0 && giorno < 32) { if (mese == 11 || mese == 4 || mese == 6 || mese == 9) { if (giorno > 30) errore = 2 } if (mese == 2) { //resto = anno % 4; resto = 1; if (((anno % 4 == 0) && (anno % 100 != 0)) || (anno % 400 == 0)) { // se è un anno bisestile imposto resto a zero altrimenti lo lascio a uno resto = 0; } if (resto == 0 && giorno > 29) errore = 2 else { if (resto != 0 && giorno > 28) errore = 2 } } } else errore = 2 } } if (errore!= 0) { if (errore == 1) { msg = "Il valore del campo <"+Label+"> non contiene un formato valido di data.\n"; msg = msg + "Immettere la data nel formato gg-mm-aaaa."; } else { msg = "Il valore del campo <"+Label+"> non contiene una data valida.\nImmettere la data"; msg = msg + " nel formato gg-mm-aaaa."; } alert(msg); Field.focus(); return false; } else { Field.value = dataInp; return true; } } // La funzione controlla la lunghezza massima (a_value) per un campo a_field di tipo textarea // e fornisce il messaggio di errore con il nome del campo (a_text) function lunghezza_x(a_field, a_value, a_text) { var v_field; v_field= findObj(a_field); if (v_field.value.length>a_value) { alert('La lunghezza del campo <'+a_text+'> supera '+a_value+'.'); v_field.focus(); v_field.select(); return false; } else return true; } function verificaDataMinoreUgualeDi(Field1, Field2,Label1, Label2) { if ((Field1.value == '') || (Field2.value == '')) return true; dataInp1 = Field1.value dataInp2 = Field2.value giorno1 = dataInp1.substring(0, 2) mese1 = dataInp1.substring(3, 5) anno1 = dataInp1.substring(6, 10) giorno2 = dataInp2.substring(0, 2) mese2 = dataInp2.substring(3, 5) anno2 = dataInp2.substring(6, 10) if (anno1 < anno2) return true; else if (anno1 == anno2) // Anni uguali: controllo sui mesi if (mese1 < mese2) return true; else if (mese1 == mese2) // Anni e mesi uguali: controllo sui giorni if (giorno1 <= giorno2) return true; msg = "La data immessa nel campo <"+ Label1 +"> non può essere posteriore a quella immessa nel campo <"+Label2+">"; alert(msg); Field1.focus(); return false; } //*************** NUOVA FUNZIONE *****************/ function Trim(StrToTrim) { // CONTROLLA CHE IL VALORE IN INPUT SIA DI TIPO STRING //alert('nella funzione'); if (typeof StrToTrim != "string") { return StrToTrim; } // CATTURA IL PRIMO CARATTERE DELLA STRINGA // PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO var StrBlank = StrToTrim.substring(0, 1); // ELIMINA LO SPAZIO VUOTO DALLA PRIMA POSIZIONE DELLA STRINGA //alert('primo while'); while (StrBlank == " ") { StrToTrim = StrToTrim.substring(1, StrToTrim.length); StrBlank = StrToTrim.substring(0, 1); } // CATTURA L'ULTIMO CARATTERE DELLA STRINGA // PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO StrBlank = StrToTrim.substring(StrToTrim.length - 1, StrToTrim.length); // ELIMINA LO SPAZIO VUOTO DALL'ULTIMA POSIZIONE DELLA STRINGA //alert('secondo while'); while (StrBlank == " ") { StrToTrim = StrToTrim.substring(0, StrToTrim.length-1); StrBlank = StrToTrim.substring(StrToTrim.length-1, StrToTrim.length); } // ELIMINA POTENZIALI SPAZI VUOTI MULTIPLI // ALL'INIZIO ED ALLA FINE DI UNA STRINGA //alert('terzo while'); //while (StrToTrim.indexOf("") != -1) //{ //alert('nel terzo while'); //StrToTrim = StrToTrim.substring(0, StrToTrim.indexOf("")); //StrToTrim += StrToTrim.substring(StrToTrim.indexOf("") + 1, StrToTrim.length); //} // RESTITUISCE IL VALORE FINALE SENZA SPAZI VUOTI DI CONTORNO //alert('prima di uscire dalla funzione'); return StrToTrim; }