You're here: Snippet Directory » HTML/JavaScript (65)
Language:

Form Checker

Language: English
Programming Language: JavaScript
Published by: aimper [not registered]
Last Update: 5/15/2006
Views: 483

Description

this set of java-scripts check if an html form is filled exactly, and return an error message in the others cases.

Code

1 //////////////////////////////////////////////////////////////////////////////////// 2 // Nome: FormChecker 3 // Autore: Augusto Fiastrelli 4 // Data: 20 aprile 2000 5 // Ultima modifica: 22 luglio 2000 (passaggio di parametri per oggetto) 6 // 7 // Scopo: 8 // controllare che i campi di una form HTML siano stati 9 // inseriti in maniera esatta. 10 // 11 // Parametri: 12 // Input 13 // Metodo: Comportamento in caso di errore (alert = "INLINE" , nuovo documento "OUTLINE"). 14 // Form: Il nome del form 15 // CampiObbligatori: Un array con i nomi dei campi obbligatori 16 // CampiData: Un array con i nomi dei campi che contengono una data 17 // CampiVal: Un array con i nomi dei campi che contengono un valore numerico 18 // CampiEmail: Un array con i nomi dei campi che contengono un e-mail 19 // CampiValSpecial: Un array con i nomi dei campi che contengono un valore 20 // numerico particolare (Es: numeri di telefono) 21 // IncValSpecial: Una stringa di caratteri che possono essere presenti nei campi ValSpecial (default: " ") 22 // isSQL: Se vero, a tutti i campi vengono raddopiati gli apici 23 // 24 // Uso: 25 // posizionare lo script nell 'head del documento e inserire la funzione 26 // CheckOk() come gestore dell' evento onSubmit per il form da controllare 27 // 28 // Include: 29 // data.js 30 // 31 //////////////////////////////////////////////////////////////////////////////////// 32 33 function FormChecker(params) { 34 35 //costanti 36 this.OBBLIGATORIO = 0; 37 this.DATA = 1; 38 this.VALORE = 2; 39 this.VALORESPECIALE = 3; 40 this.EMAIL = 4; 41 42 //propriet 43 this.Form=params.Form; 44 this.CampiObbligatori=params.CampiObbligatori; 45 this.CampiData=params.CampiData; 46 this.CampiVal=params.CampiVal; 47 this.CampiValSpecial=params.CampiValSpecial; 48 this.CampiEmail=params.CampiEmail; 49 this.IncValSpecial=params.IncValSpecial; 50 51 //metodi 52 this.CheckOk=CheckOk; 53 this.Write=WriteOut; 54 this.ControllaData=ControllaData; 55 this.ControllaVal=ControllaVal; 56 this.ControllaValSpecial=ControllaValSpecial; 57 this.ControllaEmail=ControllaEmail; 58 this.GetDaysInMonth=GetDaysInMonth; 59 60 61 if (this.CampiValSpecial != null && this.IncValSpecial + "" == "undefined") 62 this.IncValSpecial=" "; 63 64 if (params.Metodo + "" != "undefined" ) 65 if (params.Metodo.toUpperCase()=="INLINE") 66 this.Write=WriteIn; 67 68 function WriteOut(Missing) { 69 var d=document; 70 d.open(); 71 d.writeln("<H3>Attenzione, si verificato un errore durante l' invio dei dati !</H3>"); 72 d.writeln("<BR><BR>"); 73 for (var i=0;i<Missing.length;i++) { 74 if ((Missing[i][1])== this.OBBLIGATORIO) 75 d.writeln("<FONT>Il campo <FONT COLOR=red><I>" + Missing[i][0] + "</I></FONT> non stato compilato.</FONT><BR><BR>"); 76 if ( Missing[i][1]== this.DATA || 77 Missing[i][1]== this.VALORE || 78 Missing[i][1]== this.VALORESPECIALE || 79 Missing[i][1]== this.EMAIL ) 80 d.writeln("<FONT>" + Missing[i][2] + 81 " non un valore valido per il campo <FONT COLOR=red><I>" + Missing[i][0] + "</I></FONT><BR><BR>"); 82 } 83 d.writeln("<CENTER><FORM><INPUT TYPE=BUTTON onclick=\"history.back()\" VALUE=Indietro></FORM></CENTER>"); 84 d.close(); 85 } 86 87 function WriteIn(Missing) { 88 var s="Errore: i campi seguenti non possono essere nulli o sono stati inseriti in maniera errata.\n" 89 for ( i=0;i<Missing.length;i++) 90 s+="\n - " + Missing[i]; 91 alert(s); 92 } 93 94 function GetDaysInMonth(mese,anno) { 95 if (mese==2) { 96 if ( (anno % 4)==0 ) 97 return 29; 98 return 28; 99 } 100 if (mese==1 || mese==3 || mese==5 || mese==7 || mese==8 || mese==10 || mese==12 ) 101 return 31; 102 return 30; 103 } 104 105 function ControllaData(data) { 106 if (data == "") return true; 107 if (data.search(/[0-9]{1,2}[^0-9][0-9]{1,2}[^0-9][0-9]{1,4}\b/) !=0 ) return false; 108 var giorno=GetGiorno(data); 109 var mese=GetMese(data); 110 var anno=GetAnno(data); 111 //alert ("arg=#" + data+ "#"); 112 //alert ("giorno=" + giorno); 113 //alert ("mese=" + mese); 114 //alert ("anno=" + anno); 115 if (isNaN(anno)) return false; 116 if (mese == 0 || isNaN(mese) || mese > 12) return false; 117 if (giorno == 0 || isNaN(giorno) || giorno > this.GetDaysInMonth(mese,anno)) return false; 118 return true; 119 } 120 121 function ControllaVal(val) { 122 if (val == "") return true; 123 if (val.search(/[1-9][0-9]*\b/) != 0) return false; 124 return true; 125 126 } 127 128 function ControllaValSpecial(val) { 129 if (val == "") return true; 130 var ok; 131 for (var i=0;i<val.length;i++) { 132 ok=false; 133 if (!isNaN(val.charAt(i))) { 134 continue; 135 } 136 for (var y=0;y<this.IncValSpecial.length;y++) { 137 if (val.charAt(i)==this.IncValSpecial.charAt(y)) { 138 ok=true; 139 break; 140 } 141 } 142 if (!ok) return false; 143 } 144 return true; 145 } 146 147 function ControllaEmail(val) { 148 if (val=="") return true; 149 if (val.length < 6) return false; 150 var found=0; 151 for (var i=0;i<val.length;i++) { 152 if (val.charAt(i)=="@" || val.charAt(i)=="." ) found++; 153 } 154 if (found>=2) { 155 var pos=0; 156 var temp=val; 157 while( (pos=temp.search(/[.]/)) != -1 ) { 158 temp=temp.substr(pos+1); 159 } 160 return (/* controllo errato dopo l'introduzione dei domini lunghi: 161 temp.length<4 && */ 162 temp.length>1); 163 } 164 return false; 165 } 166 167 function CheckOk() { 168 var s=""; 169 var x=0; 170 var Missing=new Array(new Array(2)); 171 if (this.CampiObbligatori != null) 172 for ( var i=0;i<this.CampiObbligatori.length;i++) { 173 s=document.forms[this.Form].elements[this.CampiObbligatori[i][0]].value; 174 if (s=="") { 175 Missing[x] = new Array(2); 176 Missing[x][0]= (this.CampiObbligatori[i][1] + "" != "undefined" ? this.CampiObbligatori[i][1] : this.CampiObbligatori[i][0]); 177 Missing[x][1]=this.OBBLIGATORIO; 178 x++; 179 } 180 } 181 if (this.CampiData != null) 182 for ( i=0;i<this.CampiData.length;i++) { 183 s=document.forms[this.Form].elements[this.CampiData[i][0]].value; 184 if (!this.ControllaData(s)) { 185 Missing[x] = new Array(3); 186 Missing[x][0]= (this.CampiData[i][1] + "" != "undefined" ? this.CampiData[i][1] : this.CampiData[i][0]); 187 Missing[x][1]=this.DATA; 188 Missing[x][2]=s; 189 x++; 190 } 191 } 192 if (this.CampiVal != null) 193 for ( i=0;i<this.CampiVal.length;i++) { 194 s=document.forms[this.Form].elements[this.CampiVal[i][0]].value; 195 if (!this.ControllaVal(s)) { 196 Missing[x] = new Array(3); 197 Missing[x][0]= (this.CampiVal[i][1] + "" != "undefined" ? this.CampiVal[i][1] : this.CampiVal[i][0]); 198 Missing[x][1]=this.VALORE; 199 Missing[x][2]=s; 200 x++; 201 } 202 } 203 204 if (this.CampiValSpecial != null) 205 for ( i=0;i<this.CampiValSpecial.length;i++) { 206 s=document.forms[this.Form].elements[this.CampiValSpecial[i][0]].value; 207 if (!this.ControllaValSpecial(s)) { 208 Missing[x] = new Array(3); 209 Missing[x][0]= (this.CampiValSpecial[i][1] + "" != "undefined" ? this.CampiValSpecial[i][1] : this.CampiValSpecial[i][0]); 210 Missing[x][1]=this.VALORESPECIALE; 211 Missing[x][2]=s; 212 x++; 213 } 214 } 215 216 if (this.CampiEmail != null) 217 for ( i=0;i<this.CampiEmail.length;i++) { 218 s=document.forms[this.Form].elements[this.CampiEmail[i][0]].value; 219 if (!this.ControllaEmail(s)) { 220 Missing[x] = new Array(3); 221 Missing[x][0]= (this.CampiEmail[i][1] + "" != "undefined" ? this.CampiEmail[i][1] : this.CampiEmail[i][0]); 222 Missing[x][1]=this.EMAIL; 223 Missing[x][2]=s; 224 x++; 225 } 226 } 227 //raddoppio gli apici 228 if (isSQL && x==0) 229 for (var i=0;i<document.forms[this.Form].elements.length;i++) { 230 var p; 231 while ((p=document.forms[this.Form].elements[i].value.search("'"))!=-1) { 232 document.forms[this.Form].elements[i].value=document.forms[this.Form].elements[i].value.replace("'","''"); 233 } 234 } 235 if (x!=0) { 236 this.Write(Missing); 237 } 238 return (x==0); 239 } 240 } 241 242 //file data.js 243 function Data(giorno,mese,anno) { 244 this.giorno=giorno; 245 this.mese=mese; 246 this.anno=anno; 247 this.AddDays=AddDays; 248 this.Dump=Dump; 249 250 function GetDaysInMonth(mese,anno) { 251 if (mese==2) { 252 if ( (anno % 4)==0 ) 253 return 29; 254 return 28; 255 } 256 if (mese==1 || mese==3 || mese==5 || mese==7 || mese==8 || mese==10 || mese==12 ) 257 return 31; 258 return 30; 259 } 260 261 function AddDays(incr) { 262 this.giorno+= incr; 263 while ( this.giorno>GetDaysInMonth(this.mese,this.anno) ) { 264 this.giorno -= GetDaysInMonth(this.mese,this.anno); 265 this.mese++; 266 if ( this.mese >12 ) { 267 this.mese=1; 268 this.anno++; 269 } 270 } 271 } 272 function Compare(d1,d2) { 273 if (d1.anno == d2.anno) 274 if (d1.mese == d2.mese) 275 if (d1.giorno==d2.giorno) return 0; 276 if (d1.anno<d2.anno) return -1; 277 if (d1.anno>d2.anno) return 1; 278 if (d1.mese<d2.mese) return -1; 279 if (d1.mese>d2.mese) return 1; 280 if (d1.giorno<d2.giorno) return -1; 281 if (d1.giorno>d2.giorno) return 1; 282 } 283 284 function Compare(d2) { 285 if (this.anno == d2.anno) 286 if (this.mese == d2.mese) 287 if (this.giorno==d2.giorno) return 0; 288 if (this.anno<d2.anno) return -1; 289 if (this.anno>d2.anno) return 1; 290 if (this.mese<d2.mese) return -1; 291 if (this.mese>d2.mese) return 1; 292 if (this.giorno<d2.giorno) return -1; 293 if (this.giorno>d2.giorno) return 1; 294 } 295 296 297 function toString() { 298 var s=this.giorno "\\" + this.mese + "\\" + this.anno; 299 return s; 300 } 301 302 function Dump() { 303 alert(this.toString()); 304 } 305 } 306

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS