|
|
| | HOMEPAGE | INDICE FORUM | REGOLAMENTO | ::. | NEI PREFERITI | .:: | RSS Forum | RSS News | NEWS web | NEWS software | |
| | PUBBLICITA' | | | ARTICOLI | WIN XP | VISTA | WIN 7 | REGISTRI | SOFTWARE | MANUALI | RECENSIONI | LINUX | HUMOR | HARDWARE | DOWNLOAD | | | CERCA nel FORUM » | |
|
|
#1 |
|
Made in Japan
Top Poster
Registrato: 04-07-2001
Messaggi: 4.139
![]() |
[JS] form - bloccare l'ivio dei dati
La funzione si deve attivare al click sul pulsante submit. Per adesso se riempio solo uno dei campi i dati vengono comunque inviati e non appare neanche l'alert. Come faccio a fare in modo che appaia l'alert e che in tal caso si resti sulla pagina html e non si passi alla servlet? Codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
<script type="text/javascript">
<!--
function login(obj) {
if(document.getElementById("userlog").value == "") { //è giusto usare .value qui?
alert("Inserire il nome utente");
}
if(document.getElementbyId("passlog").value == "" { //è giusto usare .value qui?
alert("Inserire la password");
}
}
//-->
</script>
</head>
<body>
<center>
<h1>LogIn</h1>
<FORM ACTION="CheckLogin" METHOD="POST">
<table border="3" cellpadding="5">
<tr><td>Username:</td><td><input type="text" name="username" id="userlog" value="" /></td></tr>
<tr><td>Password:</td><td><input type="password" name="password" id="passlog" value="" /></td></tr>
</table>
<br>
<input type="submit" value="Login" onclick="login(this)" />
</FORM>
</center>
</body>
</html>
|
|
|
|
|
|
#2 |
|
Gold Member
Registrato: 14-06-2001
Loc.: LC
Messaggi: 2.415
![]() |
ci devi aggiungere un "return true" e un "return false" a seconda dei casi
___________________________________
![]() |
|
|
|
|
|
#3 |
|
Made in Japan
Top Poster
Registrato: 04-07-2001
Messaggi: 4.139
![]() |
Ho provato con i return, ma il form viene comunque inviato.
La funzione è diventata così: Codice:
function login(obj) {
if(obj.form.elements["userlog"].value == "") {
alert("Inserire il nome utente");
return false;
}
if(obj.form.elements["passlog"].value == "") {
alert("Inserire la password");
return true;
}
}
|
|
|
|
|
|
#4 | |
|
Senior Member
Registrato: 21-03-2008
Loc.: From Lugano (CH)...finally!
Messaggi: 330
![]() |
Quota:
Codice:
onclick="return login(this);" Occhio però che come hai scritto tu nell'ultimo posto se la password è "" il form viene inviato...nei due if io metterei return false, se non entra in nessuno dei due invece metterei return true! |
|
|
|
|
|
|
#5 |
|
Gold Member
Registrato: 14-06-2001
Loc.: LC
Messaggi: 2.415
![]() |
giusto, avevo guardato solo la funzione, non la chiamata
___________________________________
![]() |
|
|
|
|
|
#6 |
|
Made in Japan
Top Poster
Registrato: 04-07-2001
Messaggi: 4.139
![]() |
Perfetto, funziona.
Grazie |
|
|
|
|
|
#7 |
|
Hero Member
Registrato: 14-01-2005
Loc.: Rome, Italy
Messaggi: 1.132
![]() |
E se il tasto invio comparisse solo se tutti i campi fossero riempiti?
[HTML] <HTML> <HEAD> <TITLE>Form con validazione</TITLE> <style> A:link, A:visited { text-decoration: none } A:hover { text-decoration: underline } </style> <script> <!-- var iscomplete=false function checkform() { iscomplete=true for (i=0;i<=document.mioform.elements.length-1;i++) { if (document.mioform.cognome.value=="") {iscomplete=false} if (document.mioform.nome.value=="") {iscomplete=false} if (document.mioform.indirizzo.value=="") {iscomplete=false} if (document.mioform.email.value=="") {iscomplete=false} } if (!iscomplete) { window.status="Per favore compila i campi!" if (document.layers) { document.mioform.submitbut.value="NO" iscomplete=false } else if (document.all) { submitbutton.style.visibility="HIDDEN" iscomplete=false } else if (document.getElementById) { document.mioform.submitbut.value="NO" iscomplete=false } } if (iscomplete) { window.status="Adesso puoi inviare il modulo" if (document.layers) { document.mioform.submitbut.value="Invia" iscomplete=true } else if (document.all) { submitbutton.style.visibility="VISIBLE" iscomplete=true } else if (document.getElementById) { document.mioform.submitbut.value="Invia" iscomplete=true } } var timer= setTimeout("checkform()",200) } function oksubmit() { return iscomplete } // --> </script> </HEAD> <BODY BGCOLOR="white" vlink="blue" link="blue"> <br><br> <body bgcolor="#FFFFFF" onLoad="checkform()"> <center><font SIZE="2" face="Verdana">Compilare tutti i campi per inviare il modulo</font></center><br><br> <form NAME="mioform" onSubmit="return oksubmit()" ACTION="mailto ippo@pluto.it" METHOD="POST"><center> <table BORDER="0" CELLSPACING="0" CELLPADDING="1"> <tr valign="top"> <td bgcolor="CCCCCC"> <font SIZE="1" face="Verdana">Nome:</font></td> <td bgcolor="CCCCCC" align="right"><font SIZE="1" face="Verdana"><input NAME="nome" TYPE="Text" SIZE="30" MAXLENGTH="50"> </td> </tr> <tr valign="top"> <td bgcolor="CCCCCC"> <font SIZE="1" face="Verdana">Cognome:</font></td> <td bgcolor="CCCCCC"><font SIZE="1" face="Verdana"><input NAME="cognome" TYPE="Text" SIZE="30" MAXLENGTH="50"> </td> </tr> <tr valign="top"> <td bgcolor="CCCCCC"> <font SIZE="1" face="Verdana">Indirizzo:</font></td> <td bgcolor="CCCCCC" align="right"><font SIZE="1" face="Verdana"><input NAME="indirizzo" TYPE="Text" SIZE="30" MAXLENGTH="50"> </td> </tr> <tr valign="top"> <td bgcolor="CCCCCC"> <font SIZE="1" face="Verdana">Email:</font></td> <td bgcolor="CCCCCC" align="right"><font SIZE="1" face="Verdana"><input NAME="email" TYPE="Text" SIZE="30" MAXLENGTH="50"> </td> </tr> </table> </center> <SCRIPT language="JavaScript"> <!-- if (document.layers){ document.write('<DIV id="submitbutton" style="visibility:hidden">'); } else if (document.all){ document.write('<DIV id="submitbutton" style="visibility:hidden">'); } else if(document.getElementById && navigator.appName == "Netscape"){ document.write('<DIV id="submitbutton" style="visibility:hidden"></div>'); } //--> </SCRIPT> <center> <table BORDER="0" CELLSPACING="0" CELLPADDING="0"> <tr valign="top"> <td align=middle><font SIZE="1" face="Verdana"><input NAME="submitbut" TYPE="submit" VALUE=" Invia "></td> </tr> <tr> </table> </center> </div> </form> <br><br><br><br><br><br><br><br><br><br><br> </font></BODY> </HTML> [/HTML]
___________________________________
♫ m u s i c ♪ i s ♫ t h e ♪ a n s w e r ♫ "Il più piccolo dei piccoli felini è già lui stesso un vero capolavoro". (Leonardo da Vinci) Namasté! |
|
|
|
![]() |
| Utenti attualmente attivi che stanno leggendo questa discussione: 1 (0 utenti e 1 ospiti) | |
| Strumenti discussione | |
|
|
Discussioni simili
|
||||
| Discussione | Autore discussione | Forum | Risposte | Ultimo messaggio |
| [JSP] Usare form da una JSP inclusa... si può fare? | Alhazred | Programmazione | 8 | 29-07-2008 10.31.48 |
| [Vista Backup] Non si trovano i dati salvati | taniservice | Windows 7/Vista/XP/ 2003 | 1 | 25-09-2007 09.29.57 |
| Nascondere una partizione... | cworange84 | Windows 7/Vista/XP/ 2003 | 15 | 30-08-2005 10.26.56 |
| [PHP] form invio dati via email, formattati | Thor | Programmazione | 0 | 06-07-2005 18.06.30 |
| ADSL su linea solo dati: canone in arrivo? | handyman | Segnalazioni Web | 4 | 13-06-2005 15.48.21 |