Salve ho un problema sulla verifica dei campi in una form struts2 usando javascript.
Quando vado a fare il submit per salvare i dati nel db, anche se la verifica da valori sbagliati, li salva non bloccandosi come dovrebbe essere.
vi invio il codice
grazie in anticipocodice:<%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Aggiungi Contatto</title> <script type="text/javascript"> function trim(stringa) { while (stringa.substring(0, 1) == ' ') { stringa = stringa.substring(1, stringa.length); } while (stringa.substring(stringa.length - 1, stringa.length) == ' ') { stringa = stringa.substring(0, stringa.length - 1); } return stringa; } function checkForm() { var nome = document.getElementById('campo_nome').value; if (nome == '' || nome == 'Inserire il nome!') { // alert('Inserire il nome!'); document.getElementById('campo_nome').style.color = "red"; document.getElementById('campo_nome').value = "Inserire il nome!"; } var cognome = document.getElementById('campo_cognome').value; if (cognome == '' || cognome == 'Inserire il cognome!') { // alert('Inserire il cognome!'); document.getElementById('campo_cognome').style.color = "red"; document.getElementById('campo_cognome').value = "Inserire il cognome!"; } var email = document.getElementById('campo_email').value; if (email == '' || email == 'Inserire la mail!') { // alert('Inserire il numero di cellulare!'); document.getElementById('campo_email').style.color = "red"; document.getElementById('campo_email').value = "Inserire la mail!"; } var cell = document.getElementById('campo_cell').value; if (cell == '' || cell == 'Inserire il numero di cellulare!' || cell == 'Numerazione non valida!') { // alert('Inserire il numero di cellulare!'); document.getElementById('campo_cell').style.color = "red"; document.getElementById('campo_cell').value = "Inserire il numero di cellulare!"; } else { cell = trim(cell); if (cell.length != 9 && cell.length != 10) { document.getElementById('campo_cell').style.color = "red"; document.getElementById('campo_cell').value = "Numerazione non valida!"; } if (cell.substring(0, 1) != '3') { document.getElementById('campo_cell').style.color = "red"; document.getElementById('campo_cell').value = "Numerazione non valida!"; } } var tel = document.getElementById('campo_tel').value; if (tel == '' || tel == 'Inserire il numero di telefono!' || tel == 'Numerazione non valida!') { // alert('Inserire il numero di telulare!'); document.getElementById('campo_tel').style.color = "red"; document.getElementById('campo_tel').value = "Inserire il numero di telefono!"; } else { tel = trim(tel); if (tel.length != 9 && tel.length != 10) { document.getElementById('campo_tel').style.color = "red"; document.getElementById('campo_tel').value = "Numerazione non valida!"; } if (tel.substring(0, 1) != '0') { document.getElementById('campo_tel').style.color = "red"; document.getElementById('campo_tel').value = "Numerazione non valida!"; } } alert("qui"); return false; } </script> </head> <body> <h1><span style="background-color: #FFFFcc">Rubrica Web</span></h1> <h3><span style="background-color: #FFFFcc">Aggiungi contatto</span></h3> <s:form action="add" onsubmit="checkForm()"> <s:textfield id="campo_nome" label="Nome Contatto" name="contatto.nome" /> <s:textfield id="campo_cognome" label="Cognome contatto" name="contatto.cognome" /> <s:textfield id="campo_tel" label="Telefono casa" name="contatto.telefono" /> <s:textfield id="campo_cell" label="Cellulare" name="contatto.cellulare" /> <s:textfield id="campo_email" label="E-mail" name="contatto.email" /> <s:submit value="Salva Contatto" /> <s:actionerror /> </s:form> </body> </html>

Rispondi quotando

