<%@page import="mantenimiento.MantenimientoClientes"%>
<%@page import="persistencia.Empresas"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="mantenimiento.MantenimientoEmpresas"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
     <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>JSP Page</title>
        
        <script>
function validateForm()
{
    if(document.formulario.clienteid.value=="")
    {
      alert("clienteid no puede estar vacio");
      document.frm.clienteid.focus();
      
      return false;
    }
     if(document.formulario.password.value!=document.formulario.repitapassword.value)
    {
      alert("Password es diferente");
      document.frm.password.focus();
      
      return false;
    }
     if(document.formulario.nombre.value=="")
    {
      alert("nombre no puede estar vacio");
      document.frm.pwd.focus();
       
      return false;
    }
     if(document.formulario.apellido.value=="")
    {
      alert("apellido no puede estar vacio");
      document.frm.pwd.focus();
      return false;
    }
     if(document.formulario.area.value=="")
    {
      alert("area no puede estar vacio");
      document.frm.pwd.focus();
      return false;
    }
}
</script>
    </head>
    <body>
        <form  name="formulario" action="ClienteAgregado.jsp" method="POST" onSubmit="return validateForm()">
       
        <table >
            <thead>
                
            </thead>
            <tbody>
                <tr>
                    <td>ClienteId</td>
                    <td><input type="text" name="clienteid" value="" /></td>
                </tr>
                <tr>
                    <td>EmpresaId</td>
                    <td><select name="EmpresaId">
                               <%
                               
                               MantenimientoEmpresas  lista = new MantenimientoEmpresas();
                                List empresas = lista.ListaEmpresas();
                               %>
                            <%   
                              persistencia.Empresas empresa = null;
                               
                                Iterator  iterador = empresas.iterator();
                                 %>
          <% while (iterador.hasNext()){
              String opcion = iterador.next().toString();
    
    %>
            <option><% out.println(" "+ opcion); %></option> 
  
  
  
  <% }%>
             
                          
                               
             </select> </td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" name="password" value="" /></td>
                </tr>
                <tr>
                    <td>Repita Password</td>
                    <td><input type="password" name="repitapassword" value="" /></td>
                </tr>
                <tr>
                    <td>Nombre</td>
                    <td><input type="text" name="nombre" value="" /></td>
                </tr>
                <tr>
                    <td>Apellidp</td>
                    <td><input type="text" name="apellido" value="" /></td>
                </tr>
                <tr>
                    <td>Telefono</td>
                    <td><input type="text" name="telefono" value="" /></td>
                </tr>
                <tr>
                    <td>Area</td>
                    <td><input type="text" name="area" value="" /></td>
                </tr>
                <tr>
                    <td>Nivel</td>
                    <td><select name="nivel">
                                <option >1</option>
                                <option >2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select> </td>
                </tr>
            </tbody>
        </table>
       <input type="submit" name="submit" value="Submit">
 </form>
    </body>
</html>

i am using javascript to validate but if i a leave clienteid in blank the message appears BUT the page still goes to the action="ClienteAgregado.jsp" is likes the jsp ignores the javascript i dont know why.

Dani AI

Generated

A quick diagnosis and a compact fix for 's symptom (alert shows but the form still submits).

Most likely cause: a JavaScript exception is thrown after the alert and before the function can return false. In the posted function the focus calls use non‑existent references (for example document.frm and pwd), which will raise a TypeError and stop execution. Because the handler never reaches return false (or calls preventDefault()), the browser proceeds with the submit. A browser console will typically show an error like "Cannot read property '...' of undefined" — that is the clue.

Practical fixes (apply all that make sense):

  • Give the form an id and use getElementById or form.elements[...] instead of relying on implicit document.formulario or a different form name.
  • Make the field names/ids consistent and change any incorrect references (frm, pwd) to the real names.
  • Avoid naming the submit control name="submit" (it hides the form.submit method).
  • Prefer attaching a submit handler and calling event.preventDefault() to cancel submission reliably.

Small, robust example (does not duplicate the original code):

<form id="formulario" method="post" action="ClienteAgregado.jsp">
  <!-- inputs with id and name -->
  <button type="submit" id="btnSend">Submit</button>
</form>

<script>
document.getElementById('formulario').addEventListener('submit', function(e){
  var f = this;
  if (!f.elements['clienteid'].value.trim()) {
    alert('clienteid no puede estar vacio');
    f.elements['clienteid'].focus();
    e.preventDefault();
    return;
  }
  if (f.elements['password'].value !== f.elements['repitapassword'].value) {
    alert('Password es diferente');
    f.elements['password'].focus();
    e.preventDefault();
    return;
  }
});
</script>

As hinted, there is no need to "call the action from within JavaScript"; preventing the submit is sufficient — but it only works if the handler runs without runtime errors. Checking the developer console (F12) for exceptions will quickly confirm the exact problem.

it doesn't ignore it, as you say yourself: it shows the message.
call your action from within your javascript, that 'll stop the action being called if your result was 'false'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.