How to correctly validate a modal form

前端 未结 3 531
南笙
南笙 2020-12-09 11:42

I can\'t seem to get validation to work on my bootstrap modal, I have struggled with several of the examples that I have encountered.

What is the correct way to vali

相关标签:
3条回答
  • 2020-12-09 12:09

    You have two issues:

    1. You're button needs to be set to type="submit" not type="button"

    2. Your submit button should be inside your form tag.

    See working example Snippet.

    $(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"
        }
      });
    });
    <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>

    0 讨论(0)
  • 2020-12-09 12:21

    I had some similar problem. But it turned out that

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

    has to be put outside the

    $(function () { });
    

    This fixed my issue

    0 讨论(0)
  • 2020-12-09 12:24

    If you need send data via AJAX from bootstrap modal use following code:

    $("#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")
                    }            
                });
              }
    	});
    <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>

    0 讨论(0)
提交回复
热议问题