Ciao a tutti....
Avrei da chiedervi e proporvi una cosetta:
ho realizzato un oggetto che fa dei controlli su dei form (controlli di un po' tutti i tipi, dalla validità dei campi alla compilazione di campi obbligatori). Funziona e sono abbastanza contento, ma mi da un problema. Il controllo, l'invio e tutto quello che riguarda l'oggetto viene eseguito correttamente, ma se aggiungo degli eventi sui campi questi non vengono eseguiti pur non dando nessun errore. Prima di postare il codice devo dire che ho preso spunto da 2 o 3 lavori in giro, ma la mia maniacale ossessione di avere un oggetto che fa tutto mi ha fatto cadere in questo problema. Non so ancora gestire oggetti che interagiscono con eventi (dei form o delle finestre), quindi immagino che il problema nasca proprio da li. Per precisione il metodo this.start() ; sicuramente potrà essere migliorato e vi chiedo se qualcuno di voi interessa aiutarmi a migliorare il mio script, che ora vi posto.AIUTO VI PREGOcodice:function objForm(a){ var obj_H=this; // proprietà this.name=a; this.colorFocus="#FFC"; this.colorBlur="#FFF"; this.classe="req"; this.classeErrore="req2"; this.campo; this.val_OK=true; // metodi gestione proprietà this.setColorFocus=function(a){ this.colorFocus=a; }; this.getColorFocus=function(){ return this.colorFocus; }; this.setColorBlur=function(a){ this.colorBlur=a; }; this.getColorBlur=function(){ return this.colorBlur; }; this.setClasse=function(a){ this.classe=a; }; this.getClasse=function(){ return this.classe; }; this.setClasseErrore=function(a){ this.classeErrore=a; }; this.getClasseErrore=function(){ return this.classeErrore; }; // metodi this.toForm=function(a,b){ var d = eval('document.'+a+'.'+b); d.focus(); }; this.Focus=function(){ var inp=document.getElementsByTagName("input"); for(var i=0;i<inp.length;i++){ inp[i].onfocus=function(){this.style.backgroundColor=obj_H.colorFocus}; inp[i].onblur=function(){this.style.backgroundColor=obj_H.colorBlur}; } inp=document.getElementsByTagName("select"); for(var i=0;i<inp.length;i++){ inp[i].onfocus=function(){this.style.backgroundColor=obj_H.colorFocus}; inp[i].onblur=function(){this.style.backgroundColor=obj_H.colorBlur}; } inp=document.getElementsByTagName("textarea"); for(var i=0;i<inp.length;i++){ inp[i].onfocus=function(){this.style.backgroundColor=obj_H.colorFocus}; inp[i].onblur=function(){this.style.backgroundColor=obj_H.colorBlur}; } }; this.start=function(){ if(this.campo!=null) this.toForm(this.name,this.campo); if(document.getElementsByTagName) this.Focus(); if(!document.getElementById || !document.createElement) return; document[this.name].onsubmit=function(){ if(obj_H.validateForm()){ document[this.name].submit(); }else{ alert("ATTENZIONE: non tutti i campi sono stati compilati correttamente."); return(false); } } }; this.validateField=function(field, func){ var formato; var label=field.parentNode; var n=label.innerHTML.indexOf("*"); label.className=this.classe; if (field.attributes["AKKA-maxlength"] != null){ if (field.value.length > field.attributes["AKKA-maxlength"].value){ label.className=this.classeErrore; return false; } } if (field.attributes["AKKA-minlength"] != null) { if ((field.value.length < field.attributes["AKKA-minlength"].value) && (field.value.length != 0)) { label.className=this.classeErrore; return false; } } if (field.attributes["AKKA-regexp"] != null) { var re = new RegExp("^" + field.attributes["AKKA-regexp"].value + "$") if (field.value.match(re) == null) { label.className=this.classeErrore; return false; } } if (field.attributes["AKKA-type"] != null) { if((field.attributes["AKKA-type"].value.toLowerCase() == "numeric") && isNaN(field.value) && (field.value.Trim()!="")) { label.className=this.classeErrore; return false; } if((field.attributes["AKKA-type"].value.toLowerCase() == "email") && !this.isEmail(field.value) && (field.value.Trim()!="")) { label.className=this.classeErrore; return false; } if((field.attributes["AKKA-type"].value.toLowerCase() == "cap") && !this.isCap(field.value) && (field.value.Trim()!="")){ label.className=this.classeErrore; return false; } if((field.attributes["AKKA-type"].value.toLowerCase() == "piva") && !this.isPiva(field.value) && (field.value.Trim()!="")) { label.className=this.classeErrore; return false; } if((field.attributes["AKKA-type"].value.toLowerCase() == "codiceFiscale") && !this.isCodiceFiscale(field.value) && (field.value.Trim()!="")) { label.className=this.classeErrore; return false; } if((field.attributes["AKKA-type"].value.toLowerCase() == "telefono") && !this.isTel(field.value) && (field.value.Trim()!="")) { label.className=this.classeErrore; return false; } (field.attributes["AKKA-dateformat"] !=null ) ? formato=field.attributes["AKKA-dateformat"].value.toLowerCase() : formato=null; if((field.attributes["AKKA-type"].value.toLowerCase() == "data") && !this.isDate(field.value,formato) && (field.value.Trim()!="")) { label.className=this.classeErrore; return false; } } if(n!=-1){ if(field.value.Trim()=="") { label.className=this.classeErrore; return false; } } if(func!=null){ return eval(func); }else{ return true; } }; this.validateForm=function(func){ var arr=new Array("input","select","textarea"); for(var j=0;j<arr.length;j++){ var fields = document[this.name].getElementsByTagName(arr[j]); var l = fields.length; var valid = false; for(var i=0;i<l;i++){ valid = this.validateField(fields[i]); if(!valid) this.val_OK=false; } if((func!=null)&&(!eval(func))) this.val_OK=false; } }; this.isEmail=function(str){ var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)"); var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"); return (!r1.test(str) && r2.test(str)); }; this.isCap=function(a){ var r1=new RegExp("^[0-9]{5}$"); return r1.test(a); }; this.isPiva=function(a){ var r1=new RegExp("^[0-9]{11}$"); return r1.test(a); }; this.isTel=function(a){ var r1=new RegExp("/^\+?\d[0-9 .]{7,12}\d$/"); return r1.test(a); }; this.isDate=function(DateString, DateFormat) { var match, tmpDate; var gg='',mm='',aa=''; var RE = new Array(); RE['it'] = /^([0-3]\d)[\/\.]([01]\d)[\/\.](\d{4})$/g RE['en'] = /^([01]\d)[\/\.]([0-3]\d)[\/\.](\d{4})$/g RE['gen'] = /^(\d{4})[\/\.]([01]\d)[\/\.]([0-3]\d)$/g if(DateFormat==null){ alert("ATTENZIONE! Manca il formato che specifica la data."); return false; } if(!(match=DateString.match(RE[DateFormat]))) return false; match = DateString.split(/[\/\.]/); switch(DateFormat) { case "it": gg = match[0]; mm = parseInt(match[1])-1; aa = match[2]; break; case "en": mm = parseInt(match[0])-1; gg = match[1]; aa = match[2]; break; case "gen": aa = match[0]; mm = parseInt(match[1])-1; gg = match[2]; break; } tmpDate = new Date(aa, mm, gg); return (tmpDate.getMonth()==mm) && (tmpDate.getDate()==gg) && tmpDate.getFullYear()==aa; }; this.isCodiceFiscale=function(cfins){ var cf = cfins.toUpperCase(); var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/; if (!cfReg.test(cf)) return false; var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ"; var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX"; var s = 0; for( i = 1; i <= 13; i += 2 ) s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) ))); for( i = 0; i <= 14; i += 2 ) s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) ))); if ( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) ) return false; return true; } this.start(); }![]()
![]()