Visualizzazione dei risultati da 1 a 5 su 5

Discussione: Campi vuoti form

  1. #1

    Campi vuoti form

    Ciao a tutti.
    Ancora in javascript sono alle prime armi
    Volevo sapere come si fa a mostrare un alert in caso i campi di un form non siano stati riempiti:
    Codice PHP:
    <table width="300px">
    <
    tr><td width="50%">Nome:</td><td width="50%"><input type="text" name="nome" /></td></tr>
    <
    tr><td width="50%">Cognome:</td><td width="50%"><input type="text" name="cognome" /></td></tr>
    <
    tr><td width="50%">Password:</td><td width="50%"><input type="password" name="pass" /></td></tr>
    <
    tr><td width="50%">Email:</td><td width="50%"><input type="text" name="mail" /></td></tr>
    </
    table

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    puoi accedere al valore di ogni campo tramite il loro nome o il loro id utilizzando le funzioni DOM

    codice:
    document.getElementById("nomeID").value;
    Ovviamente devi dare ad ogni campo HTML un id univoco. Oppure:

    codice:
    document.getElemenstByName("nome").value;
    Per verificare che siano riempite ti basta controllare che il valore sia diverso da ""

    codice:
    if(document.getElementById("nomeID").value != "") alert("errore");

  3. #3
    Ciao e grazie per la risposta!

    Quindi devo fare una cosa così?:

    Codice PHP:
    <head>
    ....
    <
    script type="Javascript">
    if(
    document.getElementById("nome").value != ""alert("errore");
    if(
    document.getElementById("cognome").value != ""alert("errore");
    if(
    document.getElementById("pass").value != ""alert("errore");
    if(
    document.getElementById("mail").value != ""alert("errore");
    </script>
    ....
    </head>
    <body>
    <table width="300px"> 
    <tr><td width="50%">Nome:</td><td width="50%"><input type="text" id="nome" /></td></tr> 
    <tr><td width="50%">Cognome:</td><td width="50%"><input type="text" id="cognome" /></td></tr> 
    <tr><td width="50%">Password:</td><td width="50%"><input type="password" id="pass" /></td></tr> 
    <tr><td width="50%">Email:</td><td width="50%"><input type="text" id="mail" /></td></tr> 
    </table>
    </body> 

  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    codice:
    <head> 
    .... 
    <script type="Javascript"> 
    if(document.getElementById("nome").value == "") alert("errore"); 
    if(document.getElementById("cognome").value == "") alert("errore"); 
    if(document.getElementById("pass").value == "") alert("errore"); 
    if(document.getElementById("mail").value == "") alert("errore"); 
    </script> 
    .... 
    </head> 
    <body> 
    <table width="300px">  
    <tr><td width="50%">Nome:</td><td width="50%"><input type="text" id="nome" /></td></tr>  
    <tr><td width="50%">Cognome:</td><td width="50%"><input type="text" id="cognome" /></td></tr>  
    <tr><td width="50%">Password:</td><td width="50%"><input type="password" id="pass" /></td></tr>  
    <tr><td width="50%">Email:</td><td width="50%"><input type="text" id="mail" /></td></tr>  
    </table> 
    </body>
    Ti ho cambiato il codice, l'errore lo dai quando è uguale a "" e non diverso... avevo fatto un piccolo errore di distrazione, ora dovrebbe funzionare

  5. #5
    Ok,grazie sei stato gentilissimo!

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.