单击提交按钮进行HTML表单验证

时间:2018-09-28 06:56:28

标签: javascript jquery html css html5

我正在尝试创建HTML表单,并在我的提交按钮上,它必须验证是否所有字段都已填写,然后必须将详细信息附加到下面显示的表格中。但是在验证表单时,即使所有详细信息都正确填写,我也无法理解为什么将数据添加到表中

下面是正在使用的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Assignment</title>
    <style>
     table{
       width: 100%;
       margin: 25px 0;
       border-collapse: collapse;
     }
     table, th, td{
       border: 1px solid #6C220B;
     }
     table th, table td{
       padding: 8px;
       text-align: left;
     }
  </style>
    <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
            </head>
            <body>
                <div class="container">
                    <h1>Assignment on Javascript and Jquery:</h1>
                    <h2>Details form :</h2>
                    <form action="#" method="post" name="form">
                        <div class="form-group" required>
                            <label for="First Name">First Name:</label>
                            <input type="text" class="form-control" id="fn" placeholder="Enter your first name" name="firstname" required="" >
                            </div>
                            <div class="form-group">
                                <label for="Last Name">Last Name:</label>
                                <input type="text" class="form-control" id="sn" placeholder="Enter your last name" name="lastname" required="" >
                                </div>
                                <div class="form-group">
                                    <label for="Date of birth">Date of birth:</label>
                                    <input type="date" class="form-control" id="dob" placeholder="Enter your date of birth" name="dateofbirth" required="" >
                                    </div>
                                    <div class="form-group">
                                        <label for="email">Email:</label>
                                        <input type="email" class="form-control" id="em" placeholder="Enter your email id" name="email" required="" >
                                        </div>
                                        <div class="form-group">
                                            <label for="Phone Number">Phone Number:</label>
                                            <input type="text" class="form-control" id="phn" placeholder="Enter your phone number" name="phonenumber" required="" >
                                            </div>
                                            <input type="submit" class="row" value="Click to Add Row">
                                            </form>
                                            <br>
                                                <h2>The table of data :</h2>
                                                <table class="table">
                                                    <thead>
                                                        <tr>
                                                            <th>Firstname</th>
                                                            <th>Lastname</th>
                                                            <th>Date of birth</th>
                                                            <th>Email</th>
                                                            <th>Phone Number</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <tr>
                                                            <td>Durai</td>
                                                            <td>Saravanan</td>
                                                            <td>16/01/1996</td>
                                                            <td>durairaj1696@gmail.com</td>
                                                            <td>9789879736</td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                            <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
                                            <script>
     function validateForm() {
            var isValid = true;
        $('.form-group').each(function() {
           if ( $(this).val() === '' )
             isValid = false;
            });
      return isValid;
      }

     $(document).ready(function(){

         $(".row").click(function(){

             var firstname = $("#fn").val();

             var lastname = $("#sn").val();

             var dob = $("#dob").val();

             var email = $("#em").val();

             var phonenumber = $("#phn").val();

             var markup = "
                                                <tr>
                                                    <td>" + firstname + "</td>
                                                    <td>" + lastname + "</td>
                                                    <td>" + dob + "</td>
                                                    <td>" + email + "</td>
                                                    <td>" + phonenumber + "</td>
                                                </tr>";

          if(validateForm())
           {

             $("table tbody").append(markup);

           }
         });         
     });    


                                            </script>
                                        </body>
                                    </html>

3 个答案:

答案 0 :(得分:0)

我对jQuery做了一些修改。

  • 已验证输入字段的值
  • 单击按钮时,
  • 表单不再提交
  • 数据将添加到表中的正确位置

function validateForm() {
  var isValid = true;
  $('.form-group input').each(function() {
    //console.log($(this).val());
    if ($(this).val() === '')
      isValid = false;
  });
  return isValid;
}

$(document).ready(function() {

  $(".row").click(function() {
    var firstname = $("#fn").val();
    var lastname = $("#sn").val();
    var dob = $("#dob").val();
    var email = $("#em").val();
    var phonenumber = $("#phn").val();
    var markup = 
      "<tr><td>" + firstname + 
      "</td><td>" + lastname + 
      "</td><td >" + dob + 
      "</td><td>" + email + 
      "</td><td>" + phonenumber + 
      "</td></tr>";

    if (validateForm()) {
      $('.table tr:last').after(markup); /* Changed */
    }
    return false; /* Do not submit the form */
  });
});
table {
  width: 100%;
  margin: 25px 0;
  border-collapse: collapse;
}

table,
th,
td {
  border: 1px solid #6C220B;
}

table th,
table td {
  padding: 8px;
  text-align: left;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div class="container">
  <h1>Assignment on Javascript and Jquery:</h1>
  <h2>Details form :</h2>
  <form action="#" method="post" name="form">
    <div class="form-group" required>
      <label for="First Name">First Name:</label>
      <input type="text" class="form-control" id="fn" placeholder="Enter your first name" name="firstname" required="">
    </div>
    <div class="form-group">
      <label for="Last Name">Last Name:</label>
      <input type="text" class="form-control" id="sn" placeholder="Enter your last name" name="lastname" required="">
    </div>
    <div class="form-group">
      <label for="Date of birth">Date of birth:</label>
      <input type="date" class="form-control" id="dob" placeholder="Enter your date of birth" name="dateofbirth" required="">
    </div>
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control" id="em" placeholder="Enter your email id" name="email" required="">
    </div>
    <div class="form-group">
      <label for="Phone Number">Phone Number:</label>
      <input type="text" class="form-control" id="phn" placeholder="Enter your phone number" name="phonenumber" required="">
    </div>
    <input type="submit" class="row" value="Click to Add Row">
  </form>
  <br>
  <h2>The table of data :</h2>
  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Date of birth</th>
        <th>Email</th>
        <th>Phone Number</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Durai</td>
        <td>Saravanan</td>
        <td>16/01/1996</td>
        <td>durairaj1696@gmail.com</td>
        <td>9789879736</td>
      </tr>
    </tbody>
  </table>
</div>

答案 1 :(得分:0)

提交表单时,网页将重新加载。如果停止此事件,则代码将开始工作。只需以以下形式添加参数:onsubmit="return false;"

<form action="#" method="post" name="form" onsubmit="return false;">{content}

答案 2 :(得分:0)

您添加了“提交类型”按钮,为什么它不会将表格数据添加到表中,如果您将类型更改为按钮,那么它将起作用,

<input type="button" class="row" value="Click to Add Row">

必填属性仅与“提交”按钮配合使用,因此必须从javascript中手动添加必填验证。