title: JSP - Javascript form validation with jsp pages
body: <p>I developed a mvc 2 servlet application for a website.</p>
  
  <p>Everything works. But the Javascript form validation doesn't.</p>
  
  <p>Here is the code of the jsp. </p>
  
  <pre><code>&lt;%@page import="collaboration.Modello"%&gt;
  &lt;%@page import="collaboration.Utente"%&gt;
  &lt;%@page import="java.util.List"%&gt;
  &lt;%@page import="java.util.ArrayList"%&gt;
  &lt;%@page import="java.util.Iterator"%&gt;
  &lt;%@ page session="true" %&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html dir="ltr" lang="it-IT"&gt;
  &lt;% Utente user = (Utente) session.getAttribute("user"); %&gt;
  &lt;script language="Javascript"&gt;
  function verifica(modulo){
      if(modulo.keyword.value == ""){
          alert("Campo -keyword- mancante. \n Modulo non spedito. ");
          modulo.titolo.focus;
          return false;
      }
      if(modulo.autore.value == ""){
          alert("Campo -autore- mancante. \n Modulo non spedito. ");
          modulo.autore.focus;
          return false;
      }
      if(modulo.prezzo.value == ""){
          alert("Campo -prezzo- mancante. \n Modulo non spedito. ");
          modulo.prezzo.focus;
          return false;
      }
  }
  &lt;/script&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
      &lt;title&gt;Catalogo&lt;/title&gt;
      &lt;style type="text/css" media="screen"&gt;
          @import url("public/css/main.css");
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="header"&gt;
          &lt;div class="headcontent"&gt;
              &lt;h1&gt;Libreria Online / Catalogo&lt;/h1&gt;
              &lt;% if(user == null) { %&gt;
              &lt;h2&gt;Non sei loggato. &lt;a href="/secondoesercizio/loginin.jsp"&gt;Loggati&lt;/a&gt;.&lt;/h2&gt;
              &lt;% } else { %&gt;
              &lt;h2&gt;Sei loggato come: &lt;%= user.getNome() %&gt;. &lt;a href="Controllore?action=logout"&gt;Esci&lt;/a&gt;.&lt;/h2&gt;
              &lt;% } %&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div id ="content"&gt;
          &lt;div class="search"&gt;
  
          &lt;form name="ricerca" onSubmit="return verifica(this);" action="Controllore" method="POST"&gt;
              &lt;input type="hidden" name="action" value="cerca"&gt;   
              &lt;p&gt;Parola chiave : &lt;input type="text" name="keyword"&gt;&lt;/p&gt; 
              &lt;p&gt;Tipo richiesta : 
              &lt;select name=reqtype&gt;
                  &lt;option&gt;titolo&lt;/option&gt;
                  &lt;option&gt;autore&lt;/option&gt;
              &lt;/select&gt;
              &lt;/p&gt;                    
              &lt;p&gt;&lt;input type="submit" name="Submit" value="Cerca"&gt;&lt;/p&gt;
          &lt;/form&gt;
  
          &lt;span&gt;&lt;a href="/secondoesercizio/prenotazioni.jsp"&gt;Prenotazioni.&lt;/a&gt;&lt;/span&gt;
  
          &lt;/div&gt;
          &lt;table border="1"&gt;
          &lt;tr&gt;
              &lt;th&gt;Titolo&lt;/th&gt;
              &lt;th&gt;Autori&lt;/th&gt;
              &lt;th&gt;Prezzo&lt;/th&gt;
              &lt;th&gt;Prenota&lt;/th&gt;
              &lt;th&gt;Modifica&lt;/th&gt;
              &lt;th&gt;Elimina&lt;/th&gt;
          &lt;/tr&gt;
  
          &lt;%  
  
              Modello lista = (Modello) request.getAttribute("searchmodel");
              if(lista == null){
                  lista = new Modello();
              }
  
              List&lt;Modello&gt; risultato = lista.getCatalogo();
              Iterator&lt;Modello&gt; iteratore = risultato.listIterator();
              int cont = 0;
              while(iteratore.hasNext()){
                  Modello esaminato = iteratore.next();
                  cont++;
                  if(cont%2 == 0){
          %&gt;
                      &lt;tr class="greenback"&gt;
          &lt;%      } else {  %&gt;
                      &lt;tr&gt;
          &lt;%      }         %&gt;
  
              &lt;td&gt;&lt;b&gt;&lt;%= esaminato.getTitolo() %&gt;&lt;/b&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= esaminato.getAutori() %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= esaminato.getPrezzo() %&gt;&lt;/td&gt;
  
              &lt;td&gt;&lt;form name="prenotaform" onSubmit="return verifica(this);" action="Controllore" method="POST"&gt;
              &lt;input type="hidden" name="action" value="prenota"&gt;
              &lt;input type="hidden" name="idlibro" value="&lt;%= esaminato.getId() %&gt;"&gt; 
              Quantita : &lt;input type="text" name="quantita" SIZE="3" value=1&gt; 
              &lt;input type="submit" name="Submit" value="Prenota"&gt;
              &lt;/form&gt;
              &lt;/td&gt;
  
              &lt;td&gt;&lt;form action="Controllore" method="POST"&gt;
              &lt;input type="hidden" name="action" value="modifica"&gt;
              &lt;input type="hidden" name="idlibro" value="&lt;%= esaminato.getId() %&gt;"&gt;
              &lt;input type="submit" name="Submit" value="Modifica"&gt;
              &lt;/form&gt;
              &lt;/td&gt;
  
              &lt;td&gt;&lt;form action="Controllore" method="POST"&gt;
              &lt;input type="hidden" name="action" value="elimina"&gt;
              &lt;input type="hidden" name="idlibro" value="&lt;%= esaminato.getId() %&gt;"&gt;
              &lt;input type="submit" name="Submit" value="Elimina"&gt;
              &lt;/form&gt;
              &lt;/td&gt;
  
          &lt;/tr&gt;
          &lt;% } %&gt;
          &lt;/table&gt;
  
          &lt;div class="search"&gt;
              &lt;form name ="aggiungi" onSubmit="return verifica(this);" action="Controllore" method="POST"&gt;
                  &lt;input type="hidden" name="action" value="nuovolibro"&gt;
                  &lt;p&gt;Titolo : &lt;input type="text" name="titolo"&gt;&lt;/p&gt; 
                  &lt;p&gt;Autori: &lt;input type="text" name="autori"&gt;&lt;/p&gt; 
                  &lt;p&gt;Prezzo : &lt;input type="text" name="prezzo"&gt;&lt;/p&gt; 
                  &lt;p&gt;&lt;input type="submit" name="Submit" value="Aggiungi"&gt;&lt;/p&gt;
              &lt;/form&gt;
          &lt;/div&gt;
  
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I run the jsp page in the broswer. If i miss a form field like keyword in this form:</p>
  
  <pre><code>&lt;form name="ricerca" onSubmit="return verifica(this);" action="Controllore" method="POST"&gt;
              &lt;input type="hidden" name="action" value="cerca"&gt;   
              &lt;p&gt;Parola chiave : &lt;input type="text" name="keyword"&gt;&lt;/p&gt; 
              &lt;p&gt;Tipo richiesta : 
              &lt;select name=reqtype&gt;
                  &lt;option&gt;titolo&lt;/option&gt;
                  &lt;option&gt;autore&lt;/option&gt;
              &lt;/select&gt;
              &lt;/p&gt;                    
              &lt;p&gt;&lt;input type="submit" name="Submit" value="Cerca"&gt;&lt;/p&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p>The messagebox is displayed, but after it, the page just execute the Submit command and does not stop how expected. I can't give you more information, beacause i developed the website in Eclipse in local.</p>