Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it L'avatar di wegawhite
    Registrato dal
    Aug 2004
    Messaggi
    1,400

    Rendere campi obbligatori a condizione che...

    Ciao.

    Ho questo form, come faccio a far diventare il campo "File1" obbligatorio quando si seleziona il valore "Si" dalla select e non obbligatorio se si seleziona invece valore "No" ?

    Grazie in anticipo.

    codice:
    <html>
    <head>
    <script>
    
    <!--
     
    function validate(theform) 
     
    {
     
    if (theform.valore.value == "")
    { 
    alert("Non puoi andare avanti."); 
    theform.valore.focus(); 
    return false; 
    } 
     
    	return(true);
      
    }
     
    // -->
     
    </script>
    </head>
     
    <body> 
    <form action="PostURL.asp" method="POST" ENCTYPE="multipart/form-data" onSubmit="return validate(this)">
     
    <select size="1" name="valore">
                      <option>Seleziona</option>
                      <option value="True">Si</option>
                      <option value="False">No</option>
                      </select>
     
    <input type="file" name="File1" size="35">
    
    <input type="submit" value="Pulsante" name="B1">
     
    </form>
    </body>
    </html>

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    cosi:
    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Random code</title>
    <script>
    <!--
    function validate(theform){
    if ((theform.valore.options[theform.valore.selectedIndex].value == "True")&&(theform.File1.value == "")){ 
    alert("Attenziona inserisci una file per proseguire."); 
    theform.File1.focus(); 
    return false; 
    }   
    }
    // -->
    </script>
    </head>
     
    <body>
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  3. #3
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    io ho fatto così e sembra funzionare:
    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Pagina senza titolo</title>
    
        <script language="javascript" type="text/javascript">
    // <!CDATA[
    function validate(theForm )
    {
        var valore = $("valore").value;
        
        if(valore == "False")
            return true;/*select = Sì: campo File non obbligato*/
        else if (valore == "True")
        {
            var file = trim($("File1").value);
            if(file.length == 0)
            {
                alert("Il campo File è obbligato");
                return false;
            }
            else return true;
        }
        else
        {
            alert("Non puoi andare avanti.");
            $("valore").focus();
            return false;
        }
    }
    
    
    //al posto di mettere document.getElementById("div1"), mettere $("div1")
    //da prototype.js
    function $() {
      var elements = new Array();
    
      for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
          element = document.getElementById(element);
    
        if (arguments.length == 1)
          return element;
    
        elements.push(element);
      }
    
      return elements;
    }
    
    function trim(stringa)
    { 
    	stringa = stringa + "";
    	return stringa.replace(/^ */,"").replace(/ *$/,""); 
    } 
    
    // ]]>
        </script>
    
    </head>
    <body>
        <form method="post" action="a.asp" onsubmit="javascript:return validate(this);" id="form1" enctype="multipart/form-data">
            <select size="1" id="valore" name="valore">
                <option value="">Seleziona</option>
                <option value="True">Si</option>
                <option value="False">No</option>
            </select>
            <input type="file" name="File1" id="File1" onkeydown="this.blur();" />
            <input type="submit" name="B1" value="Invia" id="B1" />
        </form>
    </body>
    </html>

    ma ho notato un problema che mi disturba e forse puoi aiutarmi tu:

    se la pagina si chiama a.htm, l'istruzione alert("Il campo File è obbligato"); viene mostrata correttamente.

    Se cambio l'estensione del file in asp, file a.asp, l'alert stampa male le lettere accentate


    Ne conosci il motivo? :master:

    Pietro

  4. #4
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    un saluto ad Andrea

    mi puoi dire qualcosa sul problema lettere accentate di cui sopra? :quote:

    ciao
    Pietro

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.