2016-08-26 1 views
-1

나는이 양식이 있습니다JS 양식이 submited되어 정의되지 않은 오류가

<form onsubmit="return validarIDC()"> 
       <div class="labelBox"> 
        <div> 
         <label>Destinatario:</label> 
         <select name="destinatario"> 
          <option value="hombre">Sr.</option> 
          <option value="mujer">Sra.</option> 
         </select> 
        </div> 
        <div> 
         <label>Apellido y<br>nombre:</label> 
         <input type="text" id="nombre"> * 
        </div> 
        <div id="ubicarCampo"> 
         <label>Razón Social:</label> 
         <input type="text" name="razon"> 
        </div> 
        <div> 
         <label>Email:</label> 
         <input type="email" name="email"> * 
        </div> 
        <div> 
         <label>Teléfono:</label> 
         <input type="text" name="telefono"> * 
        </div> 
        <div> 
         <label>Celular:</label> 
         <input type="text" name="celular"> 
        </div> 
        <div> 
         <label>Via de Contacto:</label> 
         <select name="via"> 
          <option value="opc1">E-mail</option> 
          <option value="opc2">Telefono</option> 
          <option value="opc3">Correo postal</option> 
         </select> 
        </div> 
        <div> 
         <label>Comentarios:</label> 
         <textarea max="300"></textarea> 
        </div> 
        <input name="submit" type="submit" value="Enviar"> 
       </div> 
      </form> 
      <script type="text/javascript" src="script.js"></script> 

을하고 내가 입력에서 모든 데이터의 유효성을 검사하는 기능을 수행했습니다, 그리고 더 자세한 사항 후, 하나의 오류에 대한 exept 완벽하게 작동 나는 그것이 경고 (MSG)하지만 놀랍게도 모든 조건이 내가 오류의 측면에 잘 접착 "정의되지 않은"사실 수의 .. 콘솔 팝 제출 그래서

function validarIDC() { 
    var errores = []; 
    var er = /^[\w]+$/; 

    if (document.contactForm.nombre.value == "" || document.contactForm.nombre.value == null) { 
     errores.push("El nombre es obligatorio."); 
    } 
    else if (!er.test(document.contactForm.nombre.value)) { 
     errores.push("El nombre contiene caracteres no validos o  Espacios."); 
    } 
    if (document.contactForm.telefono.value == "") { 
     errores.push("Debe ingresar un telefono") 
    } 
    else if (isNaN(document.contactForm.telefono.value)) { 
      errores.push("El campo telefono contiene caracteres no validos."); 
    } 
    if (document.contactForm.email.value == "") { 
     errores.push("Debe especificar una dirección de Email."); 
    } 
    if (document.contactForm.celular.value !== "" &&                                                                 isNaN(document.contactForm.celular.value)) { 
     errores.push("El campo celular contiene caracteres no validos."); 
    } 
    if (errores.length > 0) { 
     msg = alert("Error/es: \n"); 
     for (var i = 0; i < errores.length; i++) { 
      msg += errores[i] + "\n"; 
     } 
     alert(msg); 
     return false;  
    } 
    document.contactForm.submit.disable = true; 
    alert("Los datos han sido enviados exitosamente!"); 
    return true; 
} 

: 코드, 여기에 JS입니다 로그가 아무 말도 안하고 내가 뭘 잘못하고 있는지 모르겠어. 제발 .. 누구든지 이걸 도와 줘?

+0

참고로, 텍스트 상자의 값은 null이 아니므로 이러한 검사는 쓸모가 없습니다. – epascarello

답변

2

alert()은 이 undefined이므로 아무 것도 반환하지 않으므로 변수를 초기화해야합니다.

msg = "Error/es: \n"; 

대신

msg = alert("Error/es: \n"); 

현재

가있다 .join()

var msg = "Error/es: \n" + errores.join("\n"); 
+0

은 대답을 확장합니다 :'alert()'는'prompt()'또는'confirm()'과는 달리, http://w3c.github.io/html/webappapis.html에 따라 결과를 반환하지 않습니다. # ref-for-dom-window-alert-4 –

+0

감사합니다. 그 쉬운 질문에 유감스럽게 생각합니다. 분명히 내 눈을 열어야 할 필요가 있습니다. 아직도 그 awfull 구문을 놓친 방법을 모르겠습니다. 어쨌든 당신의 도움에 감사드립니다 :) –

0
당신이 당신의 폼에 이름과 ID를 지정해야합니다

... 사용할 수 있습니다 작업 코드 :

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
<form onsubmit="return validarIDC()" name="contactForm" id="contactForm"> 
       <div class="labelBox"> 
        <div> 
         <label>Destinatario:</label> 
         <select name="destinatario"> 
          <option value="hombre">Sr.</option> 
          <option value="mujer">Sra.</option> 
         </select> 
        </div> 
        <div> 
         <label>Apellido y<br>nombre:</label> 
         <input type="text" id="nombre" name="nombre"> * 
        </div> 
        <div id="ubicarCampo"> 
         <label>Razón Social:</label> 
         <input type="text" name="razon"> 
        </div> 
        <div> 
         <label>Email:</label> 
         <input type="email" name="email"> * 
        </div> 
        <div> 
         <label>Teléfono:</label> 
         <input type="text" name="telefono"> * 
        </div> 
        <div> 
         <label>Celular:</label> 
         <input type="text" name="celular"> 
        </div> 
        <div> 
         <label>Via de Contacto:</label> 
         <select name="via"> 
          <option value="opc1">E-mail</option> 
          <option value="opc2">Telefono</option> 
          <option value="opc3">Correo postal</option> 
         </select> 
        </div> 
        <div> 
         <label>Comentarios:</label> 
         <textarea max="300"></textarea> 
        </div> 
        <input name="submit" type="submit" value="Enviar"> 
       </div> 
      </form> 
<script type="text/javascript"> 
    function validarIDC() { 
    var errores = []; 
    var er = /^[\w]+$/; 

    if (document.contactForm.nombre.value == "" || document.contactForm.nombre.value == null) { 
     errores.push("El nombre es obligatorio."); 
    } 
    else if (!er.test(document.contactForm.nombre.value)) { 
     errores.push("El nombre contiene caracteres no validos o  Espacios."); 
    } 
    if (document.contactForm.telefono.value == "") { 
     errores.push("Debe ingresar un telefono") 
    } 
    else if (isNaN(document.contactForm.telefono.value)) { 
      errores.push("El campo telefono contiene caracteres no validos."); 
    } 
    if (document.contactForm.email.value == "") { 
     errores.push("Debe especificar una dirección de Email."); 
    } 
    if (document.contactForm.celular.value !== "" &&                                                                 isNaN(document.contactForm.celular.value)) { 
     errores.push("El campo celular contiene caracteres no validos."); 
    } 
    if (errores.length > 0) { 
     msg = alert("Error/es: \n"); 
     for (var i = 0; i < errores.length; i++) { 
      msg += errores[i] + "\n"; 
     } 
     alert(msg); 
     return false;  
    } 
    document.contactForm.submit.disable = true; 
    alert("Los datos han sido enviados exitosamente!"); 
    return true; 
} 
</script> 
</body> 
</html> 
관련 문제