Visualizzazione dei risultati da 1 a 5 su 5

Discussione: controllo data

  1. #1

    controllo data

    Ciao a tutti ho bisogno di una funzione che controlla se la data è una data vera oppure no.

    ho trovato uno script però funziona in parte perchè se si scrive 56/1/2010 es l'accetta lo stesso.
    So potrei fare in modo che il giorno non superi i 31 ed il mese non superi il 12 però non so come fare per controllare l'esatezza della data perchè in questo modo se scrivo 30/2/2010 verrebbe lo stesso accettata. Non so nemmeno gestirmi con l'anno bisestile.
    Qualcuno mi sa dare un aiuto?
    codice:
    function IsDate(txtDate)
    {
    	strData = Trim(txtDate);
    	
    	var stringa = Split(txtDate, "-");
    	var giorno = stringa[0];
    	var mese = stringa[1];
    	var anno = stringa[2];
    	
    	if(giorno.length==1)
    		giorno = "0"+giorno;
    	
    	if(mese.length==1)
    		mese = "0"+mese;
    	
    	txtDate = giorno +"/" + mese +"/" + anno
    	
    	txtDate = Trim(txtDate);
    	alert("*" +txtDate+ "*")
    
        try
        {
            if (txtDate.length != 10)
            {
                return null;
            }
            else if
                 (
                     isNaN(txtDate.substring(0, 2))       ||
                           txtDate.substring(2, 3) != "/" ||
                     isNaN(txtDate.substring(3, 5))       ||
                           txtDate.substring(5, 6) != "/" ||
                     isNaN(txtDate.substring(6, 15))
                 )
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        catch (e)
        {
            return null;
        }
    }

  2. #2
    Ho aggiornato il codice con i controlli vari però mi manca ancora come controllare l'anno bisestile.
    Qualcuno mi sa aiutare?
    codice:
    function IsDate(txtDate)
    {
    	strData = Trim(txtDate);
    	
    	var stringa = Split(txtDate, "-");
    	var giorno = stringa[0];
    	var mese = stringa[1];
    	var anno = stringa[2];
    	
    	if(giorno.length==1)
    		giorno = "0"+giorno;
    	
    	if(mese.length==1)
    		mese = "0"+mese;
    	
    	txtDate = giorno +"/" + mese +"/" + anno
    	
    	txtDate = Trim(txtDate);
    	alert("*" +txtDate+ "*")
    
        try
        {
            if (txtDate.length != 10)
            {
                return null;
            }
            else if
                 (
                     isNaN(txtDate.substring(0, 2))       ||
                           txtDate.substring(2, 3) != "/" ||
                     isNaN(txtDate.substring(3, 5))       ||
                           txtDate.substring(5, 6) != "/" ||
                     isNaN(txtDate.substring(6, 15))
                 )
            {
                return false;
            }
    		else if  (
    					txtDate.substring(3, 5)==4 ||
    					txtDate.substring(3, 5)==6 ||
    					txtDate.substring(3, 5)==9 ||
    					txtDate.substring(3, 5)==11 
    				)
    		{
    			if (txtDate.substring(0, 2)>30)
    				return false;
    		}	
    		else if  (
    					txtDate.substring(3, 5)==2 
    				)
    		{
    			if (txtDate.substring(0, 2)>29)
    				return false;
    		}		
            else
            {
                return true;
            }
        }
        catch (e)
        {
            return null;
        }
    }

  3. #3
    Moderatore di JavaScript L'avatar di br1
    Registrato dal
    Jul 1999
    Messaggi
    19,998
    Non mi risulta una funzione Split() in javascript... :master:

    Alternativa sfruttando l'oggetto Date:
    codice:
    function isDate(str) { 
    	spz = str.split("/"); 
    	dat = new Date(spz[2], spz[1]-1, spz[0]); 
    	gg = dat.getDate();
    	mm = dat.getMonth() +1;
    	aa = dat.getFullYear();
    	if (aa==parseFloat(spz[2]) && mm==parseFloat(spz[1]) && gg==parseFloat(spz[0])) return dat;
    	return false;
    } 
    
    function testD(obj) {
    	RE = /[^\d^/^\-^\.^\s]/gi
    	obj.value=obj.value.replace(RE,"");
    	RE = /[\-\.\s]/gi
    	obj.value=obj.value.replace(RE,"/");
    	if (!isDate(obj.value)) {
    		alert("Data Errata")
    //		obj.focus()
    	}
    }
    ...
    <input type=text name="giorno" onblur="testD(this)">
    ciao
    Il guaio per i poveri computers e' che sono gli uomini a comandarli.

    Attenzione ai titoli delle discussioni: (ri)leggete il regolamento
    Consultate la discussione in rilievo: script / discussioni utili
    Usate la funzione di Ricerca del Forum

  4. #4
    La funzione split e replace le ho fatto al di sotto di questa funzione.
    Il tutto funziona correttamente ma se scrivo 29 febbraio 21010 questa funzione lo accetta invece non è corretta
    è solo questo il mio problema
    codice:
    function IsDate(txtDate)
    {
    	strData = Trim(txtDate);
    	
    	var stringa = Split(txtDate, "-");
    	var giorno = stringa[0];
    	var mese = stringa[1];
    	var anno = stringa[2];
    	var boolCtr = true;
    	
    	if(giorno.length==1)
    		giorno = "0"+giorno;
    	
    	if(mese.length==1)
    		mese = "0"+mese;
    	
    	if(anno.length==2)
    		boolCtr = false;
    	
    	txtDate = giorno +"/" + mese +"/" + anno
    	
    	txtDate = Trim(txtDate);
    	alert("*" +txtDate+ "*")
    
        try
        {
            if (txtDate.length != 10)
            {
                boolCtr = false;
            }
            else if
                 (
                     isNaN(txtDate.substring(0, 2))       ||
                           txtDate.substring(2, 3) != "/" ||
                     isNaN(txtDate.substring(3, 5))       ||
                           txtDate.substring(5, 6) != "/" ||
                     isNaN(txtDate.substring(6, 15))
                 )
            {
                boolCtr = false;
            }
    		else if  (
    					txtDate.substring(3, 5)==4 ||
    					txtDate.substring(3, 5)==6 ||
    					txtDate.substring(3, 5)==9 ||
    					txtDate.substring(3, 5)==11 
    				)
    		{
    			if (txtDate.substring(0, 2)>30)
    				boolCtr = false;
    		}	
    		else if  (
    					txtDate.substring(3, 5)==2 
    				)
    		{
    			if (txtDate.substring(0, 2)>29)
    				boolCtr = false;
    		}
    		if  (
    				txtDate.substring(3, 5)==1  ||
    				txtDate.substring(3, 5)==3  ||
    				txtDate.substring(3, 5)==5  ||
    				txtDate.substring(3, 5)==7  ||
    				txtDate.substring(3, 5)==8  ||
    				txtDate.substring(3, 5)==10  ||
    				txtDate.substring(3, 5)==12  
    			)
    		{
    			if (txtDate.substring(0, 2)>31)
    				boolCtr = false;
    		}
           return boolCtr; 
        }
        catch (e)
        {
            return boolCtr;
        }
    }
    
    
    function Trim(StringToTrim) { 
    // CONTROLLA CHE IL VALORE IN INPUT SIA DI TIPO STRING
    if (typeof(StringToTrim) != "string") { return StringToTrim; }
    // CATTURA IL PRIMO CARATTERE DELLA STRINGA PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
    var StringBlank = StringToTrim.substring(0, 1);
    // ELIMINA LO SPAZIO VUOTO DALLA PRIMA POSIZIONE DELLA STRINGA 
    while (StringBlank == " ") { 
    StringToTrim = StringToTrim.substring(1, StringToTrim.length); 
    StringBlank = StringToTrim.substring(0, 1);
    }
    // CATTURA L'ULTIMO CARATTERE DELLA STRINGA PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO 
    StringBlank = StringToTrim.substring(StringToTrim.length - 1, StringToTrim.length);
    // ELIMINA LO SPAZIO VUOTO DALL'ULTIMA POSIZIONE DELLA STRINGA 
    while (StringBlank == " ") {
    StringToTrim = StringToTrim.substring(0, StringToTrim.length-1); 
    StringBlank = StringToTrim.substring(StringToTrim.length-1, StringToTrim.length); 
    } 
    // ELIMINA POTENZIALI SPAZI VUOTI MULTIPLI ALL'INIZIO ED ALLA FINE DI UNA STRINGA 
    while (StringToTrim.indexOf(" ") != -1) {
    StringToTrim = StringToTrim.substring(0, StringToTrim.indexOf(" ")); 
    StringToTrim += StringToTrim.substring(StringToTrim.indexOf(" ") + 1, StringToTrim.length);
    }
    // RESTITUISCE IL VALORE FINALE SENZA SPAZI VUOTI DI CONTORNO 
    return StringToTrim;
    }
    
    
    function Replace(StringToReplace, StringToChange, StringChangedIn)
    {
        return StringToReplace.replace(StringToChange, StringChangedIn);
    }
    
    
    function Split(StringToSplit, CharToSplit) {
    return StringToSplit.split(CharToSplit); 
    }

  5. #5
    Moderatore di JavaScript L'avatar di br1
    Registrato dal
    Jul 1999
    Messaggi
    19,998
    Ripeto il consiglio: sfrutta l'oggetto Date per scoprire se l'anno e' bisestile
    codice:
    function isLeap(nAnno) {
      return ((new Date(nAnno,1,29)).getMonth()==1)
    }
    ciao
    Il guaio per i poveri computers e' che sono gli uomini a comandarli.

    Attenzione ai titoli delle discussioni: (ri)leggete il regolamento
    Consultate la discussione in rilievo: script / discussioni utili
    Usate la funzione di Ricerca del Forum

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 © 2024 vBulletin Solutions, Inc. All rights reserved.