PageSourceSearch

http://www.forestiersdalsace.fr/js/verifForm.js

js forestiersdalsace.fr collected 2026-10-01 15:10:50 UTC 17,915 bytes, 655 lines download raw bytes

1<!--
2 /* @author : xavier.ottolini[at]adelis.com
3 * @param argument id : String : paramètre id du formulaire
4 */
5 var message2 = "";
6function UniForm(id) 
7{
8	this.formulaire = document.getElementById(id);
9	this.listeChamps = new ListeChamps(this.formulaire);
10	this.valide = valideForm;
11	this.ajouteChamp = ajouteChampForm;
12	this.creer = creer;
13	this.DEFAUT = "";
14	this.ENTIER = "entier";
15	this.FLOTTANT = "flottant"
16	this.EMAIL = "email";
17	this.URL = "url";
18	this.DATE = "date";
19	this.FICHIER_IMAGE = "img";
20	this.FICHIER_TEXTE = "txt";
21}
22
23function valideForm() 
24{
25	var etat = true;
26	var taille = this.listeChamps.getLength();
27	var message = "";
28	var champFocus;
29
30	for(var i = taille; i>0; i--) 
31	{
32		var champ = this.listeChamps.getChamp(taille - i);
33//alert (champ);
34		if(!champ.valide()) 
35		{
36			if(etat) champFocus = champ;
37			message += champ.getMessage() + "\r\n";
38			etat = false;			
39		}
40	}
41	
42	if(!etat || message2) {
43		if(message2)
44		{
45			window.alert(message + message2);
46			etat = false;	
47		}
48		else
49		{
50			window.alert(message);
51			champFocus.focus();
52		}
53	}
54
55	return etat;
56}
57
58/**
59 * Méthode ajouteChamp
60 * Paramètre un champ du formulaire
61 * 
62 * @param nom , String représentant le praramètre name du champ
63 * @param regle , String représentant la règle à appliquer pour tester un champ. 
64 * Il existe une règle prédéfinie : UniForm.DEFAUT .
65 * @param requis : boolean qui vaut true si le champ est requis, false dans le cas contraire
66 * @param messageRequis : message à afficher si le champ requis n'est pas affiché
67 */
68function ajouteChampForm(nom, regle, requis, messageRequis) 
69{
70	this.listeChamps.ajouteChamp(this.creer(this.formulaire, nom, regle, requis, messageRequis));
71}
72
73/**
74 * Méthode ajouteChamp
75 * Paramètre un champ du formulaire
76 * 
77 * @param nom , String représentant le praramètre name du champ
78 * @param regle , String représentant la règle à appliquer pour tester un champ. 
79 * Il existe des règles prédéfinies : UniForm.DEFAUT, UniForm.ENTIER, UniForm.FLOTTANT, UniForm.EMAIL, UniForm.URL, UniForm.DATE, UniForm.FICHIER_IMAGE, UniForm.FICHIER_TEXTE.
80 * Ca peut être aussi une expression régulière. 
81 * Dans ce cas, attention a remplacer les '\' par des '\\' et les '/' par des '\/'. 
82 * @param requis : boolean qui vaut true si le champ est requis, false dans le cas contraire
83 * @param messageRequis : message à afficher si le champ requis n'est pas affiché
84 * @param messageRegle : message à afficher si la règle n'est pas respectée
85 */
86function ajouteChampForm(nom, regle, requis, messageRequis,messageRegle) 
87{
88	this.listeChamps.ajouteChamp(this.creer(this.formulaire, nom, regle, requis, messageRequis,messageRegle));
89}
90
91
92function creer() {
93	var a = creer.arguments;
94	var oForm = a[0];
95	var nom = a[1];
96	var regle = a[2];
97	var requis = a[3];
98	var messageRequis = a[4];
99	var messageRegle = null;
100	if(a.length == 6) messageRegle = a[5];
101
102	var unChamp = oForm.elements[nom];
103	
104	var instanceChamp;
105	if(!unChamp.length) 
106	{
107		if(unChamp.tagName.toLowerCase()=='input') 
108		{
109			if(unChamp.type.toLowerCase()=='text')
110			{
111				if(regle == this.DEFAUT) 
112				{
113
114					return instanceChamp = new ChampTexte(oForm,nom, regle ,requis, messageRequis);
115				}
116				
117				else if(regle == this.ENTIER)
118				{
119					return instanceChamp = new ChampEntier(oForm,nom, requis, messageRequis, messageRegle);
120				}
121		
122				else if(regle == this.FLOTTANT)
123				{
124					return instanceChamp = new ChampFlottant(oForm,nom, requis, messageRequis, messageRegle);
125				}
126		
127				else if(regle == this.EMAIL)
128				{
129					return instanceChamp = new ChampEmail(oForm,nom, requis, messageRequis, messageRegle);
130				}
131		
132				else if(regle == this.URL)
133				{
134					return instanceChamp = new ChampURL(oForm,nom, requis, messageRequis, messageRegle);
135				} 
136				
137				else if(regle == this.DATE)
138				{
139					return instanceChamp = new ChampDate(oForm,nom, requis, messageRequis, messageRegle);
140				} 
141				
142				else 
143				{
144					return instanceChamp = new ChampTexte(oForm,nom, regle, requis, messageRequis, messageRegle);
145				}
146				
147			} 
148			else if(unChamp.type.toLowerCase()=='file') 
149			{
150				if(regle == this.FICHIER_IMAGE) 
151				{
152					return instanceChamp = new ChampFichierImage(oForm,nom, requis, messageRequis, messageRegle);
153				}
154
155				else if(regle == this.FICHIER_TEXTE)
156				{
157					return instanceChamp = new ChampFichierTexte(oForm,nom, requis, messageRequis, messageRegle);
158				}
159
160				else 
161				{
162					return instanceChamp = new ChampFichier(oForm,nom, requis, messageRequis, messageRegle);
163				}
164			} 
165			else  if(unChamp.type.toLowerCase()=='checkbox') 
166			{
167				return instanceChamp = new ChampCase(oForm,nom, requis, messageRequis);
168			} 
169			else if(unChamp.type.toLowerCase()=='radio') 
170			{
171				return instanceChamp = new ChampRadio(oForm,nom, requis, messageRequis);
172			}
173		}
174		else if(unChamp.tagName.toLowerCase()=='select') 
175		{
176			if(unChamp.multiple) 
177			{
178				return instanceChamp = new ChampListeMultiple(oForm,nom, requis, messageRequis);
179			} 
180			else 
181			{
182				return instanceChamp = new ChampListeUnique(oForm,nom, requis, messageRequis);
183			}
184		} 
185		else if(unChamp.tagName.toLowerCase()=='textarea')
186		{
187			return instanceChamp = new ChampArea(oForm,nom, requis, messageRequis);
188		}
189	}	
190	else 
191	{
192		if(unChamp[0].tagName.toLowerCase()=='input') 
193		{
194			if(unChamp[0].type.toLowerCase()=='checkbox') 
195			{
196				return instanceChamp = new ChampsCase(oForm,nom, requis, messageRequis);
197			} 
198			else if(unChamp[0].type.toLowerCase()=='radio') 
199			{
200				return instanceChamp = new ChampsRadios(oForm,nom, requis, messageRequis);
201			}
202		}
203		else if(unChamp.tagName.toLowerCase()=='select') 
204		{
205			if(unChamp.multiple) 
206			{
207				return instanceChamp = new ChampListeMultiple(oForm,nom, requis, messageRequis);
208			} 
209			else 
210			{
211				return instanceChamp = new ChampListeUnique(oForm,nom, requis, messageRequis);
212			}
213		} 
214	}
215	
216}
217
218
219function ListeChamps()
220{
221	this.liste = new Array()
222	this.ajouteChamp = ajouteChampListe;
223	this.getChamp = getChamp;
224	this.getLength = getLength;
225}
226
227function ajouteChampListe(element)
228{
229	this.liste[this.liste.length] = element;
230}
231
232function getChamp(i) 
233{
234	return this.liste[i];
235}
236
237function getLength() 
238{
239	return this.liste.length;
240}
241
242function Champ(oForm,nom, regle, requis, messageRequis)
243{
244	this.element = oForm.elements[nom];
245	this.requis = requis;
246	this.regle = regle;
247	this.messageRequis = messageRequis;
248	this.messageRegle = null;
249	this.message = null;
250	this.getMessage = getMessage;
251}
252
253function Champ(oForm,nom, regle, requis, messageRequis, messageRegle)
254{
255	this.element = oForm.elements[nom];
256	this.regle = regle;
257	this.requis = requis;
258	this.messageRequis = messageRequis;
259	this.messageRegle = messageRegle;
260	this.message = null;
261	this.getMessage = getMessage;
262}
263
264function getMessage() 
265{
266	return this.message;
267}
268
269function ChampTexte(oForm,nom, regle, requis, messageRequis) {
270	this.parent = Champ;
271	if(regle==this.DEFAUT) {
272		regle = "(.*)";
273	}
274	this.parent(oForm,nom, regle, requis, messageRequis); 
275	this.getValeur=getValeurChampTexte;
276	this.valide = valideChampTexte;
277	this.focus = uniqueFocus;
278}
279
280function ChampTexte(oForm,nom, regle, requis, messageRequis, messageRegle) {
281	this.parent = Champ;
282	if(regle==this.DEFAUT) {
283		regle = "(.*)";
284	}
285	this.parent(oForm,nom, regle, requis, messageRequis, messageRegle); 
286	this.getValeur=getValeurChampTexte;
287	this.valide = valideChampTexte;
288	this.focus = uniqueFocus;
289}
290
291function getValeurChampTexte() 
292{
293	return this.element.value;
294}
295
296function valideChampTexte()
297{
298	var valeur = this.getValeur();
299
300	if(this.requis && ((valeur == null) || (valeur == ""))) {
301		this.message = this.messageRequis;
302		return false;
303	}
304
305	if((!this.requis) && ((valeur == null) || (valeur == ""))) {
306		return true;
307	}
308
309	var reg=new RegExp(this.regle, "gi");
310	var etat = valeur.match(reg);
311	if(!etat) this.message = this.messageRegle;
312	return etat;
313}
314
315function uniqueFocus() {
316	this.element.focus();
317}
318
319function ChampEntier(oForm,nom, requis, messageRequis, messageRegle) {
320	this.parent = Champ;
321	var regle = "^(\\+|-)?\\d+$";
322	this.parent(oForm,nom, regle, requis, messageRequis, messageRegle); 
323	this.element = oForm.elements[nom];
324	this.getValeur= getValeurChampTexte;
325	this.valide = valideChampTexte;	
326	this.focus = uniqueFocus;
327}
328
329function ChampFlottant(oForm,nom, requis, messageRequis, messageRegle) {
330	this.parent = Champ;
331	var regle = "^(\\-)?\\d*((\\.|,)\\d+)?$";
332	this.parent(oForm,nom, regle, requis, messageRequis, messageRegle); 
333	this.element = oForm.elements[nom];
334	this.getValeur=getValeurChampTexte;
335	this.valide = valideChampTexte;	
336	this.focus = uniqueFocus;
337}
338
339function ChampEmail(oForm,nom, requis, messageRequis, messageRegle) {
340	this.parent = Champ;
341	var regle = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z0-9-]+)";
342	this.parent(oForm,nom, regle, requis, messageRequis, messageRegle); 
343	this.element = oForm.elements[nom];
344	this.getValeur=getValeurChampTexte;
345	this.valide = valideChampTexte;	
346	this.focus = uniqueFocus;
347}
348
349function ChampURL(oForm,nom, requis, messageRequis, messageRegle) {
350	this.parent = Champ;
351	var regle = "^(ht|f)tp(s?)\\:\\\/\\\/[a-zA-Z0-9\\-\\._]+(\\.[a-zA-Z0-9\\-\\._]+){2,}(\\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\'\\\/\\\\\\+&%\$#_]*)?$";
352	this.parent(oForm,nom, regle, requis, messageRequis, messageRegle); 
353		this.getValeur=getValeurChampTexte;
354	this.valide = valideChampTexte;	
355	this.focus = uniqueFocus;
356}
357
358function ChampDate(oForm,nom, requis, messageRequis, messageRegle) {
359	//+tw 9 mai 2006
360	this.parent = Champ;
361	//-tw 9 mai 2006
362	this.parent(oForm,nom, "", requis, messageRequis, messageRegle); 
363	this.getValeur=getValeurChampTexte;
364	this.valide = valideChampDate;
365	this.isPasNombre = isPasNombre;
366	this.focus = uniqueFocus;
367}
368
369function valideChampDate() {
370	// This method checks if the format corresponds to JJ/MM/AAAA and if the date existes in the gregorian calendar.
371	// The separator is set up in the separateur value.
372	// return  0 if the date format is false (french format), 
373	// return 1 if the date format is correct, 
374	var valeur = this.getValeur();
375
376	if(this.requis && (valeur == "")) {
377		this.message = this.messageRequis;
378		return false;
379	}
380
381	if((!this.requis) && (valeur == "")) return true;
382
383	var etat=true;
384	var amin=1970;  // year mini
385	var amax=9999;  // year maxi
386	var separateur="/"; // separator between days/months/years
387	var j=(valeur.substring(0,2));
388	var m=(valeur.substring(3,5));
389	var a=(valeur.substring(6));
390	var ok=1;
391	if ( ((this.isPasNombre(j))||(j<1)||(j>31)) && (ok==1) ) {
392		etat=false;
393	}
394
395	if ( ((this.isPasNombre(m))||(m<1)||(m>12)) && (ok==1) ) {
396		etat=false;
397	}
398	if ( ((this.isPasNombre(a))||(a<amin)||(a>amax)) && (ok==1) ) {
399		etat=false;
400	}
401	if ( ((valeur.substring(2,3)!=separateur)||(valeur.substring(5,6)!=separateur)) && (ok==1) ) {
402		etat=false;
403	}
404	
405	if (etat) {
406		var dateTest=new Date(a,m-1,j);
407		jTest=dateTest.getDate();
408		mTest=dateTest.getMonth()+1;
409		if ( (j!=jTest)||(m!=mTest) ) etat=false;
410	} 
411	
412	if(!etat) this.message = this.messageRegle;
413	return etat;
414}
415
416function isPasNombre(n)
417{ 
418	// notNb = 0 : the value is digital ; 
419	// notNb = 1 : the value is not digital ;
420	var notNb=false; 
421	var i = 0;                // counter
422	
423	if(n.length > 0) 
424	{
425		// comparison realized on every caracters of the string 
426		// if a caracter is not between "0" and "9" then the value is not digital
427		for(var i=n.length;i>0;i--) 
428		{
429			if(isNaN(parseInt( n.charAt(n.length - i) ))) notNb =true;   
430		}
431	}
432	return notNb ;
433}
434
435
436function ChampFichierImage(oForm,nom, requis, messageRequis, messageRegle) {
437	this.parent = Champ;
438	var regle = "^(\\w|-)+\\.(((j|J)(p|P)(g|G))|((g|G)(i|I)(f|F))|((p|P)(n|N)(g|G)))$";
439	this.parent(oForm,nom, regle, requis, messageRequis, messageRegle); 
440	this.getValeur=getValeurChampTexte;
441	this.getNomFichierDuChemin = getNomFichierDuChemin;
442	this.valide = valideChampFichier;	
443	this.focus = uniqueFocus;
444}
445
446function ChampFichierTexte(oForm,nom, requis,  messageRequis, messageRegle) {
447	this.parent = Champ;
448	var regle = "^(\\w|-)+\\.(((d|D)(o|O)(c|C))|((r|R)(t|T)(f|F))|((t|T)(x|X)(t|T))|((p|P)(d|D)(f|F)))$";
449	this.parent(oForm,nom, regle, requis, messageRequis, messageRegle); 
450	this.getValeur=getValeurChampTexte;
451	this.getNomFichierDuChemin = getNomFichierDuChemin;
452	this.valide = valideChampFichier;	
453	this.focus = uniqueFocus;
454}
455
456function ChampFichier(oForm,nom, requis, messageRequis, messageRegle) {
457	this.parent = Champ;
458	var regle = "^(\\w|-)+\\.([a-z]|[A-Z]){1,4}$";
459	this.parent(oForm,nom, regle, requis, messageRequis, messageRegle); 
460	this.getValeur=getValeurChampTexte;
461	this.getNomFichierDuChemin = getNomFichierDuChemin;
462	this.valide = valideChampFichier;	
463	this.focus = uniqueFocus;
464}
465
466function getNomFichierDuChemin(valeur) {
467	var separateursExpression = Array(":","\/\/","\\\\");
468	var separateurs = Array(":","\/","\\");
469
470	for(var n=separateurs.length; n>0 ; n--) {
471		var regSeparateur = new RegExp(separateursExpression[n-1], "gi");
472		var res = valeur.match(regSeparateur);
473
474		if(res) 
475		{
476			return valeur.substring(valeur.lastIndexOf(separateurs[n-1]) + 1);
477		}
478	}
479
480	return valeur;
481}
482
483function valideChampFichier()
484{
485	var valeur = this.getNomFichierDuChemin(this.getValeur());
486
487	if(this.requis && ((valeur == null) || (valeur == ""))) {
488		this.message = this.messageRequis;
489		return false;
490	}
491
492	if((!this.requis) && ((this.getValeur() == null) || (this.getValeur() == ""))) return true;
493
494	var reg=new RegExp(this.regle, "gi");
495	var etat = valeur.match(reg);
496	if(!etat) this.message = this.messageRegle;
497	return etat;
498}
499
500function ChampCase(oForm,nom, requis, messageRequis) {
501	this.parent = Champ;
502	this.parent(oForm,nom,'', requis, messageRequis); 
503	this.getValeur=getValeurChampTexte;
504	this.valide = valideChampCase;	
505	this.focus = uniqueFocus;
506}
507
508function valideChampCase()
509{
510	if(this.requis && (!this.element.checked)) {
511		this.message = this.messageRequis;
512		return false;
513	}
514	return true;
515}
516
517function ChampRadio(oForm,nom, requis, message) {
518	this.parent = Champ;
519	this.parent(oForm,nom,'', requis, message); 
520	this.getValeur=getValeurChampTexte;
521	this.valide = valideChampRadio;	
522	this.focus = uniqueFocus;
523}
524
525
526function valideChampRadio()
527{
528	if(this.requis && (!this.element.checked)) {
529		this.message = this.messageRequis;
530		return false;
531	}
532	return true;
533}
534
535function ChampListeMultiple(oForm,nom, requis, message) {
536	this.parent = Champ;
537	this.parent(oForm,nom,'', requis, message); 
538		this.getValeur=getValeurChampListeMultiple;
539	this.valide = valideChampListeMultiple;	
540	this.focus = uniqueFocus;
541}
542
543function getValeurChampListeMultiple() 
544{
545	var valeur = new Array();
546	var n=0;
547	for (var i = this.element.options.length; i > 0; i--) 
548		if (this.element.options[this.element.options.length - i ].selected) 
549			if((this.element.options[this.element.options.length - i].value != null) && (this.element.options[this.element.options.length - i].value != '')) {
550				valeur[n]  = this.element.options[this.element.options.length - i].value;
551				n++
552			}
553	return valeur;
554}
555
556function valideChampListeMultiple()
557{
558	var valeur = this.getValeur() ;
559	if(this.requis && (valeur.length==0)) {
560		this.message = this.messageRequis;
561		return false;
562	}
563	return true;
564}
565
566function ChampListeUnique(oForm,nom, requis, message) {
567	this.parent = Champ;
568	this.parent(oForm,nom,'', requis, message); 
569	this.getValeur=getValeurChampListeUnique;
570	this.valide = valideChampListeUnique;	
571	this.focus = uniqueFocus;
572}
573
574function getValeurChampListeUnique() 
575{
576	return this.element.options[this.element.selectedIndex].value;
577}
578
579function valideChampListeUnique()
580{
581	var valeur = this.getValeur() ;
582	if(this.requis && ((valeur==null) || (valeur==''))) {
583		this.message = this.messageRequis;
584		return false;
585	}
586
587	return true;
588}
589
590function ChampArea(oForm,nom, requis, message) {
591	this.parent = Champ;
592	this.parent(oForm,nom, "(.*)", requis, message); 
593	this.getValeur=getValeurChampTexte;
594	this.valide = valideChampTexte;	
595	this.focus = uniqueFocus;
596}
597
598function ChampsCase(oForm,nom, requis, message) {
599	this.parent = Champ;
600	this.parent(oForm,nom, "", requis, message); 
601	this.getValeur=getValeurChampsCase;
602	this.valide = valideChampsCase;	
603	this.focus = multipleFocus;
604}
605
606function getValeurChampsCase() 
607{
608	var valeur = new Array();
609	var n = 0;
610	for(var i=this.element.length; i>0;i--) if (this.element[this.element.length - i].checked) {
611		valeur[n]  = this.element[this.element.length - i].value;
612		n++;
613	}
614	return valeur;
615}
616
617function valideChampsCase()
618{
619	var valeur = this.getValeur() ;
620	if(this.requis && (valeur.length==0)) {
621		this.message = this.messageRequis;
622		return false;
623	}
624	return true;
625}
626
627function multipleFocus() {
628	var elementSimple = this.element[0];
629	elementSimple.focus();
630}
631
632function ChampsRadios(oForm,nom, requis, message) {
633	this.parent = Champ;
634	this.parent(oForm,nom, "", requis, message); 
635	this.getValeur=getValeurChampsRadios;
636	this.valide = valideChampsRadios;	
637	this.focus = multipleFocus;
638}
639
640function getValeurChampsRadios() 
641{
642	for(var i=this.element.length; i>0;i--) if (this.element[this.element.length - i].checked) return this.element[this.element.length - i].value;
643	return null;
644}
645
646function valideChampsRadios()
647{
648	var valeur = this.getValeur() ;
649	if(this.requis && ((valeur==null) || (valeur==''))) {
650		this.message = this.messageRequis;
651		return false;
652	}
653	return true;
654}
655-->

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.