Visualizzazione dei risultati da 1 a 10 su 10
  1. #1

    Passaggio valori tra funzioni

    Ciao a tutti.

    Ho un problema con una funzione javascript..

    Questa funzione mi serve per riconoscere e formattare la data inserendo in vari modi il giorno mese anno..
    (es 010110 ,01012010, 1110 che darà 01/01/2010 )

    il problema è che ho un'altra funzione check_date() che deve fare il controllo se i valori sono plausibili e se l'hanno è bisestile o meno.. nel caso di data errata dovrei far in modo che venga cancellato il campo di input, ..il problema è che non riesco a fare quest'ultimo passaggio..cosa devo scrivere dopo return

    ecco gli script: (dentro ci sono alcuni alert che ho messo per fare dei test)

    codice:
    <script type="text/javascript">
    function check_date(day, month, year)
    	{	
    	if (day < 1 || day > 31) 	
    			{alert("Day must be between 1 and 31.")
    				
    		  return // CHE DEVO METTERE PER PULIRE IL CAMPO???;};
    		
    		if (month < 1 || month > 12) 
    			{ alert("Month must be between 1 and 12");
    		  return false};
    						  
    		if (month == 2) // check for february 29th		
    			{var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    				if (day>29 || (day==29 && !isleap)) 	
    					{alert("February " + y + " doesn't have " + d + " days!");
    		  return false;};
    			}
    					
    		if (month == 4 , 6 , 9 , 11 && day == 31)
    			{alert ("This month doesn't have 31 days");
    		return false;};
    	}
    </script>


    codice:
    function FormatDate(dateForm)
    	{
    	
    	 var datelength=dateForm.value.length;
    	 var date = dateForm.value;
    	// alert("valore scritto nel modulo="+date);
    	 //00alert(datelength);
    	 var datePat = /^(\d{1,2})(\/|\-|\.)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
         var matchArray = date.match(datePat);
    	 var day = date.substring(1,2);
    	 var dayvalue= day.value
    	//day = matchArray[1];
    	
    	switch (datelength) 
    			{
    			case 8://es. gg.mm.yyyy
    				
    					alert("Caso 8 digit, valore date="+date)
    				var day = date.substring(0,2);
    				var month = date.substring(2,4);
    				var year = date.substring(4,8);
    					check_date(day, month, year)
    				dateForm.value = day +"/"+ month +"/"+year;
    			break;
    			
    			case 7:	//es. g.mm.yyyyy
    					alert("Caso 7 digit, valore date="+date)
    				var day = date.substring(0,1);
    				var month = date.substring(1,3);
    				var year = date.substring(3,8);
    					check_date(day, month, year)
    				dateForm.value = "0"+day +"/"+ month +"/"+year;
    			break;
    
                  //ecc con gli altri casi
    
                                }

  2. #2
    se ho capito bene..

    vuoi pulire il campo a video in cui immetti la data!?!?

    prima del return prova a fare:
    codice:
    var inputText = document.getElementById("id_campo_a_video");
    inputText.value = '';
    Ristoranti Roma
    http://www.ristorantiromaristo.it
    info@ristorantiromaristo.it

  3. #3
    purtroppo non funziona
    l'ho messo così

    codice:
    function check_date(DateForm, day, month, year)
    	{	
    	 //alert("day="+day +"-month="+month+"-year="+year)
    	
    		if (day < 1 || day > 31) 	
    			{alert("Day must be between 1 and 31.")
    			 var inputText = document.getElementById("input");
    			 return DateForm.inputText.value = '' 
    			};
    		................................etc
    		}
    ma anche mettendo :

    var inputText = document.getElementById("input");
    inputText.value = ''
    return

    oppure
    var inputText = document.getElementById("input");
    DateForm.inputText.value = ''
    return


    ma niente.. altre idee?

  4. #4
    sei sicuro che arrivi a passare in quel punto della funzione?

    perché ho provato e mi funziona con un inputText

    che struttura hai a video?
    Ristoranti Roma
    http://www.ristorantiromaristo.it
    info@ristorantiromaristo.it

  5. #5
    se per "passare" intendi se entra dentro alla funzione check_date allora si ci passa perchè ho messo degli alert per vedere se entrava..

    cmq il file html è questo:

    codice:
    <html>
    <head>
    <Title> TEST FUNZIONE DATA</title>
    
    <script language="JavaScript" src="date_check.js" type="text/JavaScript"></script>
    
    </head>
    <body>
    <form name="form">
        <input type="text" id="input" name="modulo" maxlength="10" onChange="FormatDate(this)">
    
    </form>
    
    <button onClick=" window.location.reload( )" >aggiorna</button>
    </form>
    
    
    </body>
    </html>

  6. #6
    credo che il tuo problema stia nella FormatDate() quando torni dalla check_date() esegui questa operazione:

    codice:
    dateForm.value = day +"/"+ month +"/"+year;
    qui pur avendo pulito il campo in precedenza, lo vai a riscrivere!
    la dovresti eseguire solo se ti fai ritornare true.
    Ristoranti Roma
    http://www.ristorantiromaristo.it
    info@ristorantiromaristo.it

  7. #7
    .. ho provato a modificare questo ma non credo faccio nel modo giusto:

    codice:
    function check_date(DateForm, day, month, year)
    	{	
    	 //alert("day="+day +"-month="+month+"-year="+year)
    	
    		if (day < 1 || day > 31) 	
    			{alert("Day must be between 1 and 31.")
    			 return false;
    			}
    		else{return true}
    		
    		if (month < 1 || month > 12) 
    			{ alert("Month must be between 1 and 12")
    			  return false; 
    			}
    		else{return true}
    		
    		if (month == 2) // check for february 29th		
    			{var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    				if (day>29 || (day==29 && !isleap)) 	
    					{alert("February " + year + " doesn't have " + day + " days!")}
    			 return false; 
    			}
    		else{return true}
    		
    		if (month==4 && day==31 || month==6 && day==31 || month==9 && day==31 || month==11 && day==31)
    			{alert ("Month "+month+" doesn't have 31 days")
    			return false; }
    		else{return true}	
    	
    	}
    codice:
    function FormatDate(Form)
    	{
    	 var data= new Date()
    	 var current_year = data.getFullYear()
    	 
    	 var datelength=Form.value.length;
    	 var date = Form.value;
    	 var path = /^.$/;
    	
    	 var match_caracter = date.match(path)
    	 // if (match_caracter! null={
    		 // var temp = testo.replace(path, 
    		 // ;}
    		 
    	 var datePat = 	/^(\d{1,2})(\/|-|\\|_|\.)(\d{1,2})(\/|-|\\|_|\.)(\d{2,4})$/;   	//reg exp
    	 var datePat2=  /^(\d{1,2})(\/|-|\\|_|\.)(\d{1,2})$/;							//reg exp
    	 
         var matchArray = date.match(datePat);
    	 var matchArray1= date.match(datePat2);
    		//alert("matchArray="+matchArray)
    		//alert("matchArray1="+matchArray1)
    		
    		if(matchArray == null && matchArray1== null) 	// select type of input
    			//{alert("choose case")
    			{ choose_case(Form)
    			 return
    			};
    			
    			if (matchArray1 != null)
    			{ 	day = matchArray1[1];  		// parse date into variables
    				  if (day.length==1)
    					{day="0"+day; } //alert("day="+day)  //add 0 to day
    				month = matchArray1[3];				 
    				  if (month.length==1)  
    					{month="0"+month;} //alert("month="+month) //add 0 to month
    				year = current_year;	//	alert("year="+year)
    				 check_date(Form, day, month, year);
    				 if (true){
    					Form.value = day +"/"+ month +"/"+year;}
    				else
    					{Form.value = ""}
    			} .......ecc
    non so bene come si fa a dirgli di ritornare TRUE e come gli si fa controllare se è TRUE/FALSE..

  8. #8
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Prova a posta il link alla pagina demo pubblica.
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  9. #9
    Originariamente inviato da AndreaSTi
    purtroppo non funziona
    l'ho messo così

    codice:
    function check_date(DateForm, day, month, year)
    	{	
    	 //alert("day="+day +"-month="+month+"-year="+year)
    	
    		if (day < 1 || day > 31) 	
    			{alert("Day must be between 1 and 31.")
    			 var inputText = document.getElementById("input");
    			 return DateForm.inputText.value = '' 
    			};
    		................................etc
    		}
    ma anche mettendo :

    var inputText = document.getElementById("input");
    inputText.value = ''
    return

    oppure
    var inputText = document.getElementById("input");
    DateForm.inputText.value = ''
    return


    ma niente.. altre idee?
    return DateForm.inputText.value = ''
    non ha senso, con return si restituiscono i valori non si danno istruzioni.

    var inputText = document.getElementById("input");
    inputText.value = ''
    dovrebbe funzionare, il campo della data ha come id il valore "input"?
    <input name="data" id="input" />

  10. #10
    Originariamente inviato da cavicchiandrea
    Prova a posta il link alla pagina demo pubblica.
    ecco la pagina di prova è questa:

    http://alpequaggione.xoom.it/Funzione%20completa.html ..


    Originariamente inviato da lucavizzi
    return DateForm.inputText.value = ''
    non ha senso, con return si restituiscono i valori non si danno istruzioni.

    var inputText = document.getElementById("input");
    inputText.value = ''
    dovrebbe funzionare, il campo della data ha come id il valore "input"?
    <input name="data" id="input" />
    si si l'id del campo è input (vedi 3 post sopra) però non mi funziona..

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.