Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it L'avatar di ubbicom
    Registrato dal
    Mar 2004
    Messaggi
    1,407

    Modifica su funzione che controlla data

    Ciao a tutti.

    Ho trovato questa funzione che controlla una data inserita in un form, come la modifico per evitare che si inserisca una data superiore alla data di oggi?

    codice:
    function checkDate(dateObj)
    {
      var dt = dateObj.value.match(re);
     
      if (!dt)
        {
          alert("La data va inserita nel formato gg/mm/aaaa !");
          return(false);
        }
     
      day = dt[1];
      month = dt[2];
      year = dt[3];
     
      if (month < 1 || month > 12)
        {
          alert("Specificare un mese compreso tra 1 e 12 !");
          return(false);
        }
     
      // Determina il numero massimo di giorni nel mese month
      // Il calendario in uso è quello Gregoriano (introdotto da Papa Gregorio XIII nel 1582)
      // ed ha un ciclo di 400 anni con 97 anni bisestili anziché 100.
      // Il 1600 era bisestile, 1700, 1800 e 1900 no, il 2000 lo è, 2100, 2200, 2300 no etc.
      if (month == 2) maxDay = (!(year % 4) && ((year % 100) || !(year % 400))) ? 29 : 28;
      else maxDay = (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31;
     
      if (day < 1 || day > maxDay)
        {
          alert("Il mese di " + arrMonths[month - 1] + " non ha " + day +
                " giorni\nSpecificare un giorno compreso tra 1 e " + maxDay + " !");
     
          return(false);
        }
     
      return(true);
    }
    
    
    function convalidaForm(Qform)
    
    {
    
      if (!checkDate(Qform.data_evento))
        {
          Qform.data_evento.focus();
          Qform.data_evento.select();
          return(false);
        }
    
    //tutto OK 
    return(true);
    }

  2. #2
    Modificalo così:

    codice:
    function checkDate(dateObj)
    {
      var dt = dateObj.value.match(re);
     
      if (!dt)
        {
          alert("La data va inserita nel formato gg/mm/aaaa !");
          return(false);
        }
     
      day = dt[1];
      month = dt[2];
      year = dt[3];
     
      if (month < 1 || month > 12)
        {
          alert("Specificare un mese compreso tra 1 e 12 !");
          return(false);
        }
     
      // Determina il numero massimo di giorni nel mese month
      // Il calendario in uso è quello Gregoriano (introdotto da Papa Gregorio XIII nel 1582)
      // ed ha un ciclo di 400 anni con 97 anni bisestili anziché 100.
      // Il 1600 era bisestile, 1700, 1800 e 1900 no, il 2000 lo è, 2100, 2200, 2300 no etc.
      if (month == 2) maxDay = (!(year % 4) && ((year % 100) || !(year % 400))) ? 29 : 28;
      else maxDay = (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31;
     
      if (day < 1 || day > maxDay)
        {
          alert("Il mese di " + arrMonths[month - 1] + " non ha " + day +
                " giorni\nSpecificare un giorno compreso tra 1 e " + maxDay + " !");
     
          return(false);
        }
     
      var now = new Date();   
      now.setHours(0);
      now.setMinutes(0);
      now.setSeconds(0); 
      
      var theDate = new Date(year, month - 1, day, 0, 0, 0);
    
      if(now.getTime < theDate.getTime())
      {
         alert("Hai scelto una data futura");
         return false;
      }
    
      return(true);
    }
    
    
    function convalidaForm(Qform)
    
    {
    
      if (!checkDate(Qform.data_evento))
        {
          Qform.data_evento.focus();
          Qform.data_evento.select();
          return(false);
        }
    
    //tutto OK 
    return(true);
    }
    I DON'T Double Click!

  3. #3
    Utente di HTML.it L'avatar di ubbicom
    Registrato dal
    Mar 2004
    Messaggi
    1,407
    ti ringrazio, ma convalida anche date future, sono su IE 6:

    codice:
    <html>
    
    <head>
    <script language="javascript" type="text/javascript">
    <!--
    
    function checkDate(dateObj)
    {
      var dt = dateObj.value.match(re);
     
      if (!dt)
        {
          alert("La data va inserita nel formato gg/mm/aaaa !");
          return(false);
        }
     
      day = dt[1];
      month = dt[2];
      year = dt[3];
     
      if (month < 1 || month > 12)
        {
          alert("Specificare un mese compreso tra 1 e 12 !");
          return(false);
        }
     
      // Determina il numero massimo di giorni nel mese month
      // Il calendario in uso è quello Gregoriano (introdotto da Papa Gregorio XIII nel 1582)
      // ed ha un ciclo di 400 anni con 97 anni bisestili anziché 100.
      // Il 1600 era bisestile, 1700, 1800 e 1900 no, il 2000 lo è, 2100, 2200, 2300 no etc.
      if (month == 2) maxDay = (!(year % 4) && ((year % 100) || !(year % 400))) ? 29 : 28;
      else maxDay = (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31;
     
      if (day < 1 || day > maxDay)
        {
          alert("Il mese di " + arrMonths[month - 1] + " non ha " + day +
                " giorni\nSpecificare un giorno compreso tra 1 e " + maxDay + " !");
     
          return(false);
        }
    
      var now = new Date();   
      now.setHours(0);
      now.setMinutes(0);
      now.setSeconds(0); 
      
      var theDate = new Date(year, month - 1, day, 0, 0, 0);
    
      if(now.getTime < theDate.getTime())
      {
         alert("Hai scelto una data futura");
         return false;
      }
        
     
      return(true);
    }
    
    
    function convalidaForm(Qform)
    
    {
    
    	var i=0,Q= Qform.elements,L=Q.length;
    	
    	for (var a = 0; a < L; a++){
    	
    		var campo = Q[a];
    		if( campo.value===''){
    			campo.style.backgroundColor='orange';
    			++i;
    		}
    		else campo.style.backgroundColor='';
    	}
    	
    	if(i){
    	
    		alert('I campi segnalati sono obbligatorio in questo form.');
    		return false;
    	}     		    
         		    
      // Controlla data_evento
      if (!checkDate(Qform.data_evento))
        {
          Qform.data_evento.focus();
          Qform.data_evento.select();
          return(false);
        }
    
       
    //tutto OK 
    return(true);
    }
    
    
    //-->
    </script>
    </head>
    
    <body>
    
    <form method="POST" action="" onsubmit="return(convalidaForm(this));">
      
    
    <input type="text" name="data_evento" size="20">
      <input type="submit" value="Invia" name="B1"><input type="reset" value="Reimposta" name="B2"></p>
    </form>
    
    </body>
    
    </html>

  4. #4
    mancano le parentesi tonde...

    if(now.getTime < theDate.getTime())


    dovrebbe essere

    if(now.getTime() < theDate.getTime())
    I DON'T Double Click!

  5. #5
    Utente di HTML.it L'avatar di ubbicom
    Registrato dal
    Mar 2004
    Messaggi
    1,407
    Continua a validare date future:

    codice:
    <html>
    
    <head>
    <script language="javascript" type="text/javascript">
    <!--
    
    function checkDate(dateObj)
    {
      var dt = dateObj.value.match(re);
     
      if (!dt)
        {
          alert("La data va inserita nel formato gg/mm/aaaa !");
          return(false);
        }
     
      day = dt[1];
      month = dt[2];
      year = dt[3];
     
      if (month < 1 || month > 12)
        {
          alert("Specificare un mese compreso tra 1 e 12 !");
          return(false);
        }
     
      // Determina il numero massimo di giorni nel mese month
      // Il calendario in uso è quello Gregoriano (introdotto da Papa Gregorio XIII nel 1582)
      // ed ha un ciclo di 400 anni con 97 anni bisestili anziché 100.
      // Il 1600 era bisestile, 1700, 1800 e 1900 no, il 2000 lo è, 2100, 2200, 2300 no etc.
      if (month == 2) maxDay = (!(year % 4) && ((year % 100) || !(year % 400))) ? 29 : 28;
      else maxDay = (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31;
     
      if (day < 1 || day > maxDay)
        {
          alert("Il mese di " + arrMonths[month - 1] + " non ha " + day +
                " giorni\nSpecificare un giorno compreso tra 1 e " + maxDay + " !");
     
          return(false);
        }
    
      var now = new Date();   
      now.setHours(0);
      now.setMinutes(0);
      now.setSeconds(0); 
      
      var theDate = new Date(year, month - 1, day, 0, 0, 0);
    
      if(now.getTime() < theDate.getTime())
      {
         alert("Hai scelto una data futura");
         return false;
      }
        
     
      return(true);
    }
    
    
    function convalidaForm(Qform)
    
    {
    
    	var i=0,Q= Qform.elements,L=Q.length;
    	
    	for (var a = 0; a < L; a++){
    	
    		var campo = Q[a];
    		if( campo.value===''){
    			campo.style.backgroundColor='orange';
    			++i;
    		}
    		else campo.style.backgroundColor='';
    	}
    	
    	if(i){
    	
    		alert('I campi segnalati sono obbligatorio in questo form.');
    		return false;
    	}     		    
         		    
      // Controlla data_evento
      if (!checkDate(Qform.data_evento))
        {
          Qform.data_evento.focus();
          Qform.data_evento.select();
          return(false);
        }
    
       
    //tutto OK 
    return(true);
    }
    
    
    //-->
    </script>
    </head>
    
    <body>
    
    <form method="POST" action="" onsubmit="return(convalidaForm(this));">
      
    
    <input type="text" name="data_evento" size="20">
      <input type="submit" value="Invia" name="B1"><input type="reset" value="Reimposta" name="B2"></p>
    </form>
    
    </body>
    
    </html>

  6. #6
    ed "re" che cos'è?
    I DON'T Double Click!

  7. #7
    Utente di HTML.it L'avatar di ubbicom
    Registrato dal
    Mar 2004
    Messaggi
    1,407
    Originariamente inviato da artorius
    ed "re" che cos'è?

    codice:
    var re = new RegExp("^(\\d{2})/(\\d{2})/(\\d{4})$", "");
     
    var arrMonths = new Array("Gennaio", "Febbraio", "Marzo", "Aprile",
                              "Maggio", "Giugno", "Luglio", "Agosto",
                              "Settembre", "Ottobre", "Novembre", "Dicembre");

  8. #8
    non so che dirti ho fatto copia incolla del tuo codice e a me funziona...
    I DON'T Double Click!

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.