2016-08-26 4 views
-1

submited ist habe ich diese Form:JS undefiniert Fehler, wenn das Formular

<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> 

und ich habe eine Funktion getan, um alle Daten von den Eingängen zu validieren, und es funktioniert perfekt außer für einen Fehler, mehr Details nach dem Code, hier ist die JS:

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; 
} 

Also, wenn ich eintragen erscheint die alert (msg) aber überraschend, wenn eine Bedingung erfüllt seine i „undefiniert“ auf einer Seite der Fehler klebten erhalten .. die Konsole log sagt nichts und ich weiß nicht, was mache ich falsch .. bitte kann jemand mir dabei helfen?

+0

FYI, ein Textfeld Wert wird nicht Null sein, so dass diese Prüfungen nutzlos sind. – epascarello

Antwort

2

Sie müssen die Variable initialisieren, da alert() nichts zurückgibt msg ist undefined.

msg = "Error/es: \n"; 

statt

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

können Sie verwenden .join()

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

, um die Antwort zu erweitern: im Gegensatz zu 'prompt()' oder 'confirm()' gibt 'alert()' kein Ergebnis gemäß der Spezifikation zurück: http://w3c.github.io/html/webappapis.html # ref-for-dom-window-alert-4 –

+0

Ok danke, das hat geholfen, und tut mir leid für diese einfache Frage .. ich muss meine Augen öffnen, weiß immer noch nicht, wie ich diese schreckliche Syntax vermisst habe. Danke trotzdem für deine Hilfe :) –

0

Sie einen Namen und eine ID zu Ihrem Formular angeben sollten ...

hier a Arbeitscode:

<!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>