Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 26
  1. #1

    Return false che non funziona?????

    Salve a tutti. Grazie alle tante discussione lette ho scritto questo codice che serve da validatore per il mio form:
    codice:
    \\Codice javascript messo tra gli HEAD
    
    <script language="javascript">
    // <!CDATA[
    
    function Button1_onclick(){
    
       var s_data = document.getElementById("dataprotocollo").value;
    
       if ((document.step1.annodich.value)==""){
       alert("Inserire l'anno dichiarato!");
       document.step1.annodich.focus();
       return false;}
    
       if ((document.step1.annodich.value)!=""){
    	if (!/[0-9]+/.test(document.step1.annodich.value)){
    	alert("Il campo 'Anno dichiarato' deve contenere solo numeri!");
    	document.step1.annodich.focus();
    	return false;}
       }
    
       if ((document.step1.nseriale.value)==""){
       alert("Inserire un numero seriale!");
       document.step1.nseriale.focus();
       return false;}
    
       if ((document.step1.nseriale.value)!=""){
    	if (!/[0-9]+/.test(document.step1.nseriale.value)){
    	alert("Il campo 'N.Seriale' deve contenere solo numeri!");
    	document.step1.nseriale.focus();
    	return false;}
       }
    
       if ((document.step1.protocollo.value)==""){
       alert("Inserire un numero di protocollo!");
       document.step1.protocollo.focus();
       return false;}
    
       if ((document.step1.dataprotocollo.value)==""){
       alert("Inserire una data di protocollo!");
       document.step1.dataprotocollo.focus();
       return false;}
    
    
        if(IsDate(s_data))
        {
            a_data = s_data.split(/\//g);
            if(parseInt(a_data[2]) <= 99) 
            {
                alert("L'anno della data deve essere di 4 cifre");
    	    document.step1.dataprotocollo.focus();
                return false;
            }
            
            var d_data = new Date(a_data[2], a_data[1]-1, a_data[0]);
            var d_oggi = new Date(); d_oggi = new Date(d_oggi.getFullYear(), d_oggi.getMonth(), d_oggi.getDate());
            if(d_data > d_oggi){
                alert("La data è maggiore della odierna");
    	    document.step1.dataprotocollo.focus();
    	    return false;}
        }
    }
    
    
    //------------------------------------------------
    //valida la stringa dateStr
    //------------------------------------------------
    
    
    function IsDate(dateStr)
    {
    
    var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{4})$/;
    var matchArray = dateStr.match(datePat);
    
      if ((document.step1.dataprotocollo.value)!=""){
    
    	if (matchArray == null){
    	alert("Digitare la data nel formato dd/mm/yyyy");
    	return false;}
    
    	day = matchArray[1];
    	month = matchArray[3];
    	year = matchArray[5];
    
    	if (month < 1 || month > 12){
    	alert("I mesi dell'anno sono 12!");
    	return false;}
    
    	if (day < 1 || day > 31){
    	alert("I giorni del mese vanno dall' 1 al 31!");
    	return false;}
    
    	if ((month==4 || month==6 || month==9 || month==11) && day==31){
    	alert("Il mese "+month+" non ha 31 giorni!");
    	return false;}
    
    	if (month == 2){
    	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    	if (day > 29 || (day==29 && !isleap)){
    	alert("Febbraio " + year + " non ha " + day + " giorni!");
    	return false;}
    	}
    
      }
    
    }
    // ]]>
    </script>
    
    \\l'evento viene messo nel form così
    <form onsubmit="return Button1_onclick();">
    Ora tutto funziona a meraviglia e tutti i controlli li fà a dovere. L'unico problem lo riscontro quando mi và a controllare l'esattezza della data ed invece di bloccare l'invio, dopo aver riscontrato l'errore e mostrata la finestra d'allerta, il form viene inviato cmq.

    Spero che qualche anima pia sappia aiutarmi.. :_D

    Ciao Davide

  2. #2
    Moderatore di JavaScript L'avatar di br1
    Registrato dal
    Jul 1999
    Messaggi
    19,998
    Perche' la funzione IsDate() restituisce un valore che non viene utilizzato dalla funzione chiamante.... modifica:
    codice:
        if(IsDate(s_data))
        {
             // altri controlli....     
        } else return false;
    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

  3. #3
    codice:
    function Button1_onclick(){
    
       var s_data = document.getElementById("dataprotocollo").value;
    
       if ((document.step1.annodich.value)==""){
       alert("Inserire l'anno dichiarato!");
       document.step1.annodich.focus();
       return false;}
    
       if ((document.step1.annodich.value)!=""){
    	if (!/[0-9]+/.test(document.step1.annodich.value)){
    	alert("Il campo 'Anno dichiarato' deve contenere solo numeri!");
    	document.step1.annodich.focus();
    	return false;}
       }
    
       if ((document.step1.nseriale.value)==""){
       alert("Inserire un numero seriale!");
       document.step1.nseriale.focus();
       return false;}
    
       if ((document.step1.nseriale.value)!=""){
    	if (!/[0-9]+/.test(document.step1.nseriale.value)){
    	alert("Il campo 'N.Seriale' deve contenere solo numeri!");
    	document.step1.nseriale.focus();
    	return false;}
       }
    
       if ((document.step1.protocollo.value)==""){
       alert("Inserire un numero di protocollo!");
       document.step1.protocollo.focus();
       return false;}
    
       if ((document.step1.dataprotocollo.value)==""){
       alert("Inserire una data di protocollo!");
       document.step1.dataprotocollo.focus();
       return false;}
    
    
        if(IsDate(s_data))
        {
            a_data = s_data.split(/\//g);
            if(parseInt(a_data[2]) <= 99) 
            {
                alert("L'anno della data deve essere di 4 cifre");
    	    document.step1.dataprotocollo.focus();
                return false;
            }
            
            var d_data = new Date(a_data[2], a_data[1]-1, a_data[0]);
            var d_oggi = new Date(); d_oggi = new Date(d_oggi.getFullYear(), d_oggi.getMonth(), d_oggi.getDate());
            if(d_data > d_oggi){
                alert("La data è maggiore della odierna");
    	    document.step1.dataprotocollo.focus();
    	    return false;}
        }
        else {  // se non e' una data valida, ritorno false
            document.step1.dataprotocollo.focus(); 
            return false;
        }
        return true;  // se passa tutti i controlli, per pulizia, ritorno true
    }
    HTH
    Zappa
    [PC:Presario 2515EU][Cpu:P4@2.3GHz][Ram: 512M][HDU:80G@5400 RPM]
    [Wireless:LinkSys][OS: Ubuntu 9.04 - Jaunty Jackalope]

  4. #4
    Capito il problema teorico. So che sembrerò ridicolo ma in che punto và inserita la If che mi hai segnalato?

    Grazie ancora

  5. #5
    homezappa ma la verifica della data non è la stessa mia. Come inserire la verifica If tua nel mio codice?


  6. #6
    Beh, avendoti corretto io la Button1_onclick(), basta che la copi, no?

    Zappa
    [PC:Presario 2515EU][Cpu:P4@2.3GHz][Ram: 512M][HDU:80G@5400 RPM]
    [Wireless:LinkSys][OS: Ubuntu 9.04 - Jaunty Jackalope]

  7. #7
    Grazie ragazzi per l'aiuto

    Posto il codice completo casmai servisse a qualcuno:

    codice:
    <script language="javascript">
    // <!CDATA[
    
    function Button1_onclick(){
    
       var s_data = document.getElementById("dataprotocollo").value;
    
       if ((document.step1.annodich.value)==""){
       alert("Inserire l'anno dichiarato!");
       document.step1.annodich.focus();
       return false;}
    
       if ((document.step1.annodich.value)!=""){
    	if (!/[0-9]+/.test(document.step1.annodich.value)){
    	alert("Il campo 'Anno dichiarato' deve contenere solo numeri!");
    	document.step1.annodich.focus();
    	return false;}
       }
    
       if ((document.step1.nseriale.value)==""){
       alert("Inserire un numero seriale!");
       document.step1.nseriale.focus();
       return false;}
    
       if ((document.step1.nseriale.value)!=""){
    	if (!/[0-9]+/.test(document.step1.nseriale.value)){
    	alert("Il campo 'N.Seriale' deve contenere solo numeri!");
    	document.step1.nseriale.focus();
    	return false;}
       }
    
       if ((document.step1.protocollo.value)==""){
       alert("Inserire un numero di protocollo!");
       document.step1.protocollo.focus();
       return false;}
    
       if ((document.step1.dataprotocollo.value)==""){
       alert("Inserire una data di protocollo!");
       document.step1.dataprotocollo.focus();
       return false;}
    
    
        if(IsDate(s_data))
        {
            a_data = s_data.split(/\//g);
            if(parseInt(a_data[2]) <= 99) 
            {
                alert("L'anno della data deve essere di 4 cifre");
    	    document.step1.dataprotocollo.focus();
                return false;
            }
            
            var d_data = new Date(a_data[2], a_data[1]-1, a_data[0]);
            var d_oggi = new Date(); d_oggi = new Date(d_oggi.getFullYear(), d_oggi.getMonth(), d_oggi.getDate());
            if(d_data > d_oggi){
                alert("La data è maggiore della odierna");
    	    document.step1.dataprotocollo.focus();
    	    return false;}
        }
        else {  // se non e' una data valida, ritorno false
            document.step1.dataprotocollo.focus(); 
            return false;
        }
        return true;  // se passa tutti i controlli, per pulizia, ritorno true
    }
    
    
    //------------------------------------------------
    //valida la stringa dateStr
    //------------------------------------------------
    
    
    function IsDate(dateStr)
    {
    
    var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{4})$/;
    var matchArray = dateStr.match(datePat);
    
      if ((document.step1.dataprotocollo.value)!=""){
    
    	if (matchArray == null){
    	alert("Digitare la data nel formato dd/mm/yyyy");
    	return false;}
    
    	day = matchArray[1];
    	month = matchArray[3];
    	year = matchArray[5];
    
    	if (month < 1 || month > 12){
    	alert("I mesi dell'anno sono 12!");
    	return false;}
    
    	if (day < 1 || day > 31){
    	alert("I giorni del mese vanno dall' 1 al 31!");
    	return false;}
    
    	if ((month==4 || month==6 || month==9 || month==11) && day==31){
    	alert("Il mese "+month+" non ha 31 giorni!");
    	return false;}
    
    	if (month == 2){
    	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    	if (day > 29 || (day==29 && !isleap)){
    	alert("Febbraio " + year + " non ha " + day + " giorni!");
    	return false;}
    	}
    }
    
    return true;}
    
    // ]]>
    </script>

  8. #8
    Tornando nell'argomento risolto con questa discussione, come fare lo stesso controllo per due campi data dello stesso form? Ci sto provando a ragionare ma ancora non mi funziona il codice.

    Spero che qualcuno mi aiuti

  9. #9
    Questo codice che non funziona, l'ho provato a scrivere. Spero che vedendolo qualcuno posso aiutarmi a correggerlo:

    codice:
    <script language="javascript">
    // <!CDATA[
    
    function Button1_onclick(){
    
       var s_data = document.getElementById("datanascita").value;
       var x_data = document.getElementById("datanascitar").value;
    
       if ((document.step2.cap.value)!=""){
    	if (!/[0-9]+/.test(document.step2.cap.value)){
    	alert("Il campo 'Cap' deve contenere solo numeri!");
    	document.step2.cap.focus();
    	return false;}
       }
    
       if ((document.step2.telefono.value)!=""){
    	if (!/[0-9]+/.test(document.step2.telefono.value)){
    	alert("Il campo 'Telefono' deve contenere solo numeri!");
    	document.step2.telefono.focus();
    	return false;}
       }
    
       if ((document.step2.partiva.value)!=""){
    	if (!/[0-9]+/.test(document.step2.partiva.value)){
    	alert("Il campo 'Partita Iva' deve contenere solo numeri!");
    	document.step2.partiva.focus();
    	return false;}
       }
    
       if ((document.step2.capsede.value)!=""){
    	if (!/[0-9]+/.test(document.step2.capsede.value)){
    	alert("Il campo 'Cap' deve contenere solo numeri!");
    	document.step2.capsede.focus();
    	return false;}
       }
    
       if ((document.step2.telefonosede.value)!=""){
    	if (!/[0-9]+/.test(document.step2.telefonosede.value)){
    	alert("Il campo 'Telefono' deve contenere solo numeri!");
    	document.step2.telefonosede.focus();
    	return false;}
       }
    
       if ((document.step2.capr.value)!=""){
    	if (!/[0-9]+/.test(document.step2.capr.value)){
    	alert("Il campo 'Cap' deve contenere solo numeri!");
    	document.step2.capr.focus();
    	return false;}
       }
    
       if ((document.step2.telefonor.value)!=""){
    	if (!/[0-9]+/.test(document.step2.telefonor.value)){
    	alert("Il campo 'Telefono' deve contenere solo numeri!");
    	document.step2.telefonor.focus();
    	return false;}
       }
    
    if(IsDate(s_data))
        {
            a_data = s_data.split(/\//g);
            if(parseInt(a_data[2]) <= 99) 
            {
                alert("L'anno della data deve essere di 4 cifre");
    	    document.step2.datanascita.focus();
                return false;
            }
            
            var d_data = new Date(a_data[2], a_data[1]-1, a_data[0]);
            var d_oggi = new Date(); d_oggi = new Date(d_oggi.getFullYear(), d_oggi.getMonth(), d_oggi.getDate());
            if(d_data > d_oggi){
                alert("La data è maggiore della odierna");
    	    document.step2.datanascita.focus();
    	    return false;}
        }
        else if(IsDate(x_data)) {
      
            a_data = x_data.split(/\//g);
            if(parseInt(a_data[2]) <= 99)
    
            {
                alert("L'anno della data deve essere di 4 cifre");
    	    document.step2.datanascitar.focus();
                return false;
            }
            
            var d_data = new Date(a_data[2], a_data[1]-1, a_data[0]);
            var d_oggi = new Date(); d_oggi = new Date(d_oggi.getFullYear(), d_oggi.getMonth(), d_oggi.getDate());
            if(d_data > d_oggi){
                alert("La data è maggiore della odierna");
    	    document.step2.datanascitar.focus();
    	    return false;}
        }
    
        else {  // se non e' una data valida, ritorno false 
    
            return false;
        }
    
        return true;  // se passa tutti i controlli, per pulizia, ritorno true
    
        }
    
    
    //------------------------------------------------
    //valida la stringa dateStr
    //------------------------------------------------
    
    
    function IsDate(dateStr)
    {
    
    var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{4})$/;
    var matchArray = dateStr.match(datePat);
    
      if ((document.step2.datanascita.value)!=""){
    
    	if (matchArray == null){
    	alert("Digitare la data nel formato dd/mm/yyyy");
    	return false;}
    
    	day = matchArray[1];
    	month = matchArray[3];
    	year = matchArray[5];
    
    	if (month < 1 || month > 12){
    	alert("I mesi dell'anno sono 12!");
    	return false;}
    
    	if (day < 1 || day > 31){
    	alert("I giorni del mese vanno dall' 1 al 31!");
    	return false;}
    
    	if ((month==4 || month==6 || month==9 || month==11) && day==31){
    	alert("Il mese "+month+" non ha 31 giorni!");
    	return false;}
    
    	if (month == 2){
    	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    	if (day > 29 || (day==29 && !isleap)){
    	alert("Febbraio " + year + " non ha " + day + " giorni!");
    	return false;}
    	}
      }
    
      else if ((document.step2.datanascitar.value)!=""){
    
    	if (matchArray == null){
    	alert("Digitare la data nel formato dd/mm/yyyy");
    	return false;}
    
    	day = matchArray[1];
    	month = matchArray[3];
    	year = matchArray[5];
    
    	if (month < 1 || month > 12){
    	alert("I mesi dell'anno sono 12!");
    	return false;}
    
    	if (day < 1 || day > 31){
    	alert("I giorni del mese vanno dall' 1 al 31!");
    	return false;}
    
    	if ((month==4 || month==6 || month==9 || month==11) && day==31){
    	alert("Il mese "+month+" non ha 31 giorni!");
    	return false;}
    
    	if (month == 2){
    	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    	if (day > 29 || (day==29 && !isleap)){
    	alert("Febbraio " + year + " non ha " + day + " giorni!");
    	return false;}
    	}
      }
    
    }
    
    return true;}
    
    // ]]>
    </script>

  10. #10
    Ulteriore modifica dello script:

    codice:
    <script language="javascript">
    // <!CDATA[
    
    function Button1_onclick(){
    
       var s_data = document.getElementById("datanascita").value;
       var x_data = document.getElementById("datanascitar").value;
    
       if ((document.step2.cap.value)!=""){
    	if (!/[0-9]+/.test(document.step2.cap.value)){
    	alert("Il campo 'Cap' deve contenere solo numeri!");
    	document.step2.cap.focus();
    	return false;}
       }
    
       if ((document.step2.telefono.value)!=""){
    	if (!/[0-9]+/.test(document.step2.telefono.value)){
    	alert("Il campo 'Telefono' deve contenere solo numeri!");
    	document.step2.telefono.focus();
    	return false;}
       }
    
       if ((document.step2.partiva.value)!=""){
    	if (!/[0-9]+/.test(document.step2.partiva.value)){
    	alert("Il campo 'Partita Iva' deve contenere solo numeri!");
    	document.step2.partiva.focus();
    	return false;}
       }
    
       if ((document.step2.capsede.value)!=""){
    	if (!/[0-9]+/.test(document.step2.capsede.value)){
    	alert("Il campo 'Cap' deve contenere solo numeri!");
    	document.step2.capsede.focus();
    	return false;}
       }
    
       if ((document.step2.telefonosede.value)!=""){
    	if (!/[0-9]+/.test(document.step2.telefonosede.value)){
    	alert("Il campo 'Telefono' deve contenere solo numeri!");
    	document.step2.telefonosede.focus();
    	return false;}
       }
    
       if ((document.step2.capr.value)!=""){
    	if (!/[0-9]+/.test(document.step2.capr.value)){
    	alert("Il campo 'Cap' deve contenere solo numeri!");
    	document.step2.capr.focus();
    	return false;}
       }
    
       if ((document.step2.telefonor.value)!=""){
    	if (!/[0-9]+/.test(document.step2.telefonor.value)){
    	alert("Il campo 'Telefono' deve contenere solo numeri!");
    	document.step2.telefonor.focus();
    	return false;}
       }
    
    if(IsDate(s_data)){
    
            a_data = s_data.split(/\//g);
            if(parseInt(a_data[2]) <= 99) 
            {
                alert("L'anno della data deve essere di 4 cifre");
    	    document.step2.datanascita.focus();
                return false;
            }
            
            var d_data = new Date(a_data[2], a_data[1]-1, a_data[0]);
            var d_oggi = new Date(); d_oggi = new Date(d_oggi.getFullYear(), d_oggi.getMonth(), d_oggi.getDate());
            if(d_data > d_oggi){
                alert("La data è maggiore della odierna");
    	    document.step2.datanascita.focus();
    	    return false;}
    
    }else if(IsDate(s_data) == false || IsDate(s_data) == null){
    
    	If(IsDate(x_data)){
      
            b_data = x_data.split(/\//g);
            if(parseInt(b_data[2]) <= 99)
            {
                alert("L'anno della data deve essere di 4 cifre");
    	    document.step2.datanascitar.focus();
                return false;
            }
            
            var h_data = new Date(b_data[2], b_data[1]-1, b_data[0]);
            var h_oggi = new Date(); h_oggi = new Date(h_oggi.getFullYear(), h_oggi.getMonth(), h_oggi.getDate());
            if(h_data > h_oggi){
                alert("La data è maggiore della odierna");
    	    document.step2.datanascitar.focus();
    	    return false;}
    	}
        
    }else{  // se non e' una data valida, ritorno false 
    
            return false;
    }
    
        return true;  // se passa tutti i controlli, per pulizia, ritorno true
    
        }
    
    
    //------------------------------------------------
    //valida la stringa dateStr
    //------------------------------------------------
    
    
    function IsDate(dateStr)
    {
    
    var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{4})$/;
    var matchArray = dateStr.match(datePat);
    
      if ((document.step2.datanascita.value)!=""){
    
    	if (matchArray == null){
    	alert("Digitare la data nel formato dd/mm/yyyy");
    	return false;}
    
    	day = matchArray[1];
    	month = matchArray[3];
    	year = matchArray[5];
    
    	if (month < 1 || month > 12){
    	alert("I mesi dell'anno sono 12!");
    	return false;}
    
    	if (day < 1 || day > 31){
    	alert("I giorni del mese vanno dall' 1 al 31!");
    	return false;}
    
    	if ((month==4 || month==6 || month==9 || month==11) && day==31){
    	alert("Il mese "+month+" non ha 31 giorni!");
    	return false;}
    
    	if (month == 2){
    	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    	if (day > 29 || (day==29 && !isleap)){
    	alert("Febbraio " + year + " non ha " + day + " giorni!");
    	return false;}
    	}
      }
    
      else if ((document.step2.datanascitar.value)!=""){
    
    	if (matchArray == null){
    	alert("Digitare la data nel formato dd/mm/yyyy");
    	return false;}
    
    	day = matchArray[1];
    	month = matchArray[3];
    	year = matchArray[5];
    
    	if (month < 1 || month > 12){
    	alert("I mesi dell'anno sono 12!");
    	return false;}
    
    	if (day < 1 || day > 31){
    	alert("I giorni del mese vanno dall' 1 al 31!");
    	return false;}
    
    	if ((month==4 || month==6 || month==9 || month==11) && day==31){
    	alert("Il mese "+month+" non ha 31 giorni!");
    	return false;}
    
    	if (month == 2){
    	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    	if (day > 29 || (day==29 && !isleap)){
    	alert("Febbraio " + year + " non ha " + day + " giorni!");
    	return false;}
    	}
      }
    
    }
    
    return true;}
    
    // ]]>
    </script>
    Continua a non funzionare Uffff

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.