如何正确验证模态表单

时间:2016-01-25 18:21:42

标签: javascript jquery twitter-bootstrap twitter-bootstrap-3 bootstrap-modal

我似乎无法通过我的bootstrap模式进行验证,我一直在努力处理我遇到的几个例子。

验证引导模式的正确方法是什么?

我的HTML:

 <div class="modal fade" id="addMyModal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Add Stuff</h4>
            </div>
            <div class="modal-body">
                <form class="form-control" role="form" id="newModalForm">
                    <div class="form-group">
                        <label class="control-label col-md-3" for="email">A p Name:</label>
                        <div class="col-md-9">
                            <input type="text" class="form-control required error" id="pName" name="pName" placeholder="Enter a p name" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3" for="email">Action:</label>
                        <div class="col-md-9">
                            <input type="text" class="form-control" id="action" placeholder="Enter and action">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" id="btnSaveIt">Save</button>
                <button type="button" class="btn btn-default" id="btnCloseIt" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

我的Javascript:

$(function () {        

    $("#newModalForm").validate({
        rules: {
            pName: {
                required: true,
                minlength: 8
            },
            action: "required"
        },
        messages: {
            pName: {
                required: "Please enter some data",
                minlength: "Your data must be at least 8 characters"
            },
            action: "Please provide some data"
        }
    });
});

基于我的代码,当我单击模态上的保存按钮时似乎没有发生任何事情。我正在使用jquery.validate.js脚本。

有人能指出我正确的方向吗?

4 个答案:

答案 0 :(得分:10)

您有两个问题:

  1. 您的按钮需要设置为type="submit"而不是type="button"

  2. 您的提交按钮应位于form标记内。

  3. 请参阅工作示例摘录。

    &#13;
    &#13;
    $(function() {
    
      $("#newModalForm").validate({
        rules: {
          pName: {
            required: true,
            minlength: 8
          },
          action: "required"
        },
        messages: {
          pName: {
            required: "Please enter some data",
            minlength: "Your data must be at least 8 characters"
          },
          action: "Please provide some data"
        }
      });
    });
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#addMyModal">Open Modal</button>
    <div class="modal fade" id="addMyModal" role="dialog">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Add Stuff</h4>
          </div>
          <div class="modal-body">
            <form role="form" id="newModalForm">
              <div class="form-group">
                <label class="control-label col-md-3" for="email">A p Name:</label>
                <div class="col-md-9">
                  <input type="text" class="form-control" id="pName" name="pName" placeholder="Enter a p name" require/>
                </div>
              </div>
              <div class="form-group">
                <label class="control-label col-md-3" for="email">Action:</label>
                <div class="col-md-9">
                  <input type="text" class="form-control" id="action" name="action" placeholder="Enter and action" require>
                </div>
              </div>
              <div class="modal-footer">
                <button type="submit" class="btn btn-success" id="btnSaveIt">Save</button>
                <button type="button" class="btn btn-default" id="btnCloseIt" data-dismiss="modal">Close</button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:2)

如果您需要通过AJAX从bootstrap模式发送数据,请使用以下代码:

&#13;
&#13;
$("#addMyModal form").validate({
		rules: {
		 	Name: {
			 	required: true,
			 	minlength: 2
		 },
		 	Email: {
			 	required: true,
			 	minlength: 5
		 },
		 	Phone: {
				required: true,
				minlength: 12
		 }
		},
		messages: {
		 	Name: {
			 	required: "Обязательное поле",
			 	minlength: "Минимальная длинна Имени 2 символа"
		 },
		 	Email: {
			 	required: "Обязательное поле",
			 	minlength: "Минимальная длинна Email 5 символов",
			 	email: "Пожалуйста введите корректный email адрес."
		 },
		 	Phone: {
			 	required: "Обязательное поле",
			 	minlength: "Минимальная длинна Телефона 11 символов"
		 }
		},
		submitHandler: function(form) {
            $.ajax({
                url: './pay-vip.php',
                type: 'POST',
                data: $(form).serialize(),
                success: function(response) {
                    location.replace(location + "pay-vip.php");
                    alert("Send mail")
                }            
            });
          }
	});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#addMyModal">Open Modal</button>
<div class="modal fade" id="addMyModal" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Add Stuff</h4>
      </div>
      <div class="modal-body">
        <form role="form" id="newModalForm">
          <div class="form-group">
            <label class="control-label col-md-3" for="email">A p Name:</label>
            <div class="col-md-9">
              <input type="text" class="form-control" id="pName" name="pName" placeholder="Enter a p name" require/>
            </div>
          </div>
          <div class="form-group">
            <label class="control-label col-md-3" for="email">Action:</label>
            <div class="col-md-9">
              <input type="text" class="form-control" id="action" name="action" placeholder="Enter and action" require>
            </div>
          </div>
          <div class="modal-footer">
            <button type="submit" class="btn btn-success" id="btnSaveIt">Save</button>
            <button type="button" class="btn btn-default" id="btnCloseIt" data-dismiss="modal">Close</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我有一些类似的问题。但事实证明

$("#newModalForm").validate({ ... }); 

必须放在

之外
$(function () { });

这解决了我的问题

答案 3 :(得分:0)

&#13;
&#13;
$(function() {

  $("#newModalForm").validate({
    rules: {
      pName: {
        required: true,
        minlength: 8
      },
      action: "required"
    },
    messages: {
      pName: {
        required: "Please enter some data",
        minlength: "Your data must be at least 8 characters"
      },
      action: "Please provide some data"
    }
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#addMyModal">Open Modal</button>
<div class="modal fade" id="addMyModal" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Add Stuff</h4>
      </div>
      <div class="modal-body">
        <form role="form" id="newModalForm">
          <div class="form-group">
            <label class="control-label col-md-3" for="email">A p Name:</label>
            <div class="col-md-9">
              <input type="text" class="form-control" id="pName" name="pName" placeholder="Enter a p name" require/>
            </div>
          </div>
          <div class="form-group">
            <label class="control-label col-md-3" for="email">Action:</label>
            <div class="col-md-9">
              <input type="text" class="form-control" id="action" name="action" placeholder="Enter and action" require>
            </div>
          </div>
          <div class="modal-footer">
            <button type="submit" class="btn btn-success" id="btnSaveIt">Save</button>
            <button type="button" class="btn btn-default" id="btnCloseIt" data-dismiss="modal">Close</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;