2017-11-05 4 views
0

Es gibt Kommentar Bootstrap mitValidation Form - JS

<!DOCTYPE html> 
<html> 
<head> 
    <title>Comments</title> 
</head> 
<body> 
    <form id="commentForm" name="commentForm" onsubmit="return(validate());"> 
     <div class="row"> 
      <div class="col-sm-12 form-group"> 
       <textarea class="form-control" id="comments" name="comments" placeholder="Leave comment..." rows="3"></textarea><br> 
       <button class="btn pull-right" type="submit">Send</button> 
      </div> 
     </div> 
    </form> 
</body> 
</html> 

Und ich schrieb einen JS Validierung

<script type="javascript"> 
    function validate() 
    { 

    if(commentForm.comments.value == "") 
    { 
     alert("Please leave your comment!"); 
     commentForm.comments.focus() ; 
     return false; 
    } 
    </script> 

es wird prüfen, ob eine Zeile leer ist, aber es funktioniert nicht bei allen. Hilf mir bitte, wie kann ich es reparieren?

Antwort

0

Fix type-Attribut des Skript-Tag text/javascript und } der validate Funktion verpasst add

0

Sie haben das Kommentarformular Element abzurufen etwa so:

function validate() { 
 
    // Actually get the DOM element on which you can test value 
 
    var commentForm = document.getElementById('commentForm'); 
 
    
 
    if (commentForm.comments.value === '') { 
 
    window.alert('Please leave your comment!'); 
 
    commentForm.comments.focus() ; 
 
    return false; 
 
    } 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Comments</title> 
 
</head> 
 
<body> 
 
    <form id="commentForm" name="commentForm" onsubmit="return(validate());"> 
 
    <div class="row"> 
 
     <div class="col-sm-12 form-group"> 
 
     <textarea class="form-control" id="comments" name="comments" placeholder="Leave comment..." rows="3"></textarea><br> 
 
     <button class="btn pull-right" type="submit">Send</button> 
 
     </div> 
 
    </div> 
 
    </form> 
 
</body> 
 
</html>