验证表格 - JS

时间:2017-11-05 19:00:10

标签: javascript html validation bootstrap-4

有使用Bootstrap的评论表

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

我写了一个JS验证

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

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

它将检查一行是否为空,但它根本不起作用。求助于我,我该如何解决?

2 个答案:

答案 0 :(得分:0)

你必须像这样检索commentForm元素:

&#13;
&#13;
          <?php
             echo("Names: " . $_POST['dbNames'] . "<br/>\n");
          ?>
&#13;
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;
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

将脚本代码的类型属性修复为Object[] nodes = Node.returnArray(head); Node<Integer> returned1 = (Node<Integer>) nodes[0]; Node<Integer> returned2 = (Node<Integer>) nodes[1]; 并添加text/javascript函数的遗漏}