0

I developed a mvc 2 servlet application for a website.

Everything works. But the Javascript form validation doesn't.

Here is the code of the jsp.

<%@page import="collaboration.Modello"%>
<%@page import="collaboration.Utente"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@ page session="true" %>
<!DOCTYPE html>
<html dir="ltr" lang="it-IT">
<% Utente user = (Utente) session.getAttribute("user"); %>
<script language="Javascript">
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;
    }
}
</script>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Catalogo</title>
    <style type="text/css" media="screen">
        @import url("public/css/main.css");
    </style>
</head>
<body>
    <div id="header">
        <div class="headcontent">
            <h1>Libreria Online / Catalogo</h1>
            <% if(user == null) { %>
            <h2>Non sei loggato. <a href="/secondoesercizio/loginin.jsp">Loggati</a>.</h2>
            <% } else { %>
            <h2>Sei loggato come: <%= user.getNome() %>. <a href="Controllore?action=logout">Esci</a>.</h2>
            <% } %>
        </div>
    </div>
    <div id ="content">
        <div class="search">

        <form name="ricerca" onSubmit="return verifica(this);" action="Controllore" method="POST">
            <input type="hidden" name="action" value="cerca">   
            <p>Parola chiave : <input type="text" name="keyword"></p> 
            <p>Tipo richiesta : 
            <select name=reqtype>
                <option>titolo</option>
                <option>autore</option>
            </select>
            </p>                    
            <p><input type="submit" name="Submit" value="Cerca"></p>
        </form>

        <span><a href="/secondoesercizio/prenotazioni.jsp">Prenotazioni.</a></span>

        </div>
        <table border="1">
        <tr>
            <th>Titolo</th>
            <th>Autori</th>
            <th>Prezzo</th>
            <th>Prenota</th>
            <th>Modifica</th>
            <th>Elimina</th>
        </tr>

        <%  

            Modello lista = (Modello) request.getAttribute("searchmodel");
            if(lista == null){
                lista = new Modello();
            }

            List<Modello> risultato = lista.getCatalogo();
            Iterator<Modello> iteratore = risultato.listIterator();
            int cont = 0;
            while(iteratore.hasNext()){
                Modello esaminato = iteratore.next();
                cont++;
                if(cont%2 == 0){
        %>
                    <tr class="greenback">
        <%      } else {  %>
                    <tr>
        <%      }         %>

            <td><b><%= esaminato.getTitolo() %></b></td>
            <td><%= esaminato.getAutori() %></td>
            <td><%= esaminato.getPrezzo() %></td>

            <td><form name="prenotaform" onSubmit="return verifica(this);" action="Controllore" method="POST">
            <input type="hidden" name="action" value="prenota">
            <input type="hidden" name="idlibro" value="<%= esaminato.getId() %>"> 
            Quantita : <input type="text" name="quantita" SIZE="3" value=1> 
            <input type="submit" name="Submit" value="Prenota">
            </form>
            </td>

            <td><form action="Controllore" method="POST">
            <input type="hidden" name="action" value="modifica">
            <input type="hidden" name="idlibro" value="<%= esaminato.getId() %>">
            <input type="submit" name="Submit" value="Modifica">
            </form>
            </td>

            <td><form action="Controllore" method="POST">
            <input type="hidden" name="action" value="elimina">
            <input type="hidden" name="idlibro" value="<%= esaminato.getId() %>">
            <input type="submit" name="Submit" value="Elimina">
            </form>
            </td>

        </tr>
        <% } %>
        </table>

        <div class="search">
            <form name ="aggiungi" onSubmit="return verifica(this);" action="Controllore" method="POST">
                <input type="hidden" name="action" value="nuovolibro">
                <p>Titolo : <input type="text" name="titolo"></p> 
                <p>Autori: <input type="text" name="autori"></p> 
                <p>Prezzo : <input type="text" name="prezzo"></p> 
                <p><input type="submit" name="Submit" value="Aggiungi"></p>
            </form>
        </div>

    </div>
</body>
</html>

I run the jsp page in the broswer. If i miss a form field like keyword in this form:

<form name="ricerca" onSubmit="return verifica(this);" action="Controllore" method="POST">
            <input type="hidden" name="action" value="cerca">   
            <p>Parola chiave : <input type="text" name="keyword"></p> 
            <p>Tipo richiesta : 
            <select name=reqtype>
                <option>titolo</option>
                <option>autore</option>
            </select>
            </p>                    
            <p><input type="submit" name="Submit" value="Cerca"></p>
        </form>

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.

1 Answer 1

2
modulo.prezzo.focus;

http://www.w3schools.com/jsref/met_html_focus.asp

focus is a function. It will called as focus().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.