Bootstrap验证不适用于表单提交

时间:2016-03-19 21:12:24

标签: javascript jquery validation

所以我是bootstrap的新手,到目前为止,我已经按照我想要的方式工作了。我创建了一个联系表单,它使用模态表单并使用Mail函数通过PHP发送电子邮件。

我已经创建了一个脚本来对表单进行验证。如果验证通过,我希望发送电子邮件。

目前,当用户提交空白表单时,验证无效并且电子邮件已发送。

请记住,我是使用bootstrap的新手。

HTML

<div id="ContactUs" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
         <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;  </button>
                <h4 class="modal-title" id="myModalLabel">Contact Us</h4>
                <p><b>XXXX</b><br>
                    Company Phone: XXXXX
                </p>
              </div>
              <form id="contactForm" method="post" action="scripts/email.php">
                  <div class="modal-body">
                        <div class="form-group">
                            <label for="name">Name</label>
                            <input type="text" name="name" id="name" class="form-control" placeholder="Please enter your full name here.">
                            <label for="name">Email</label>
                            <input type="text" name="email" id="email" class="form-control" placeholder="Please enter your email address here.">
                            <label for="name">Subject</label>
                            <input type="text" name="subject" id="subject" class="form-control" placeholder="Please enter your subject here.">
                            <label for="message">Message</label>
                            <textarea name="message" class="form-control" placeholder="Please enter your message here."></textarea>
                        </div>
                    </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Submit</button>
                  </div>
              </form>
            </div>
          </div>
        </div>

脚本

    $(document).ready(function()
    {
        $validator = $("#contactForm").validate({
            errorClass:'error',
            validClass:'success',
            errorElement:'span',
            highlight: function (element, errorClass, validClass) { 
$(element).parents("div[class='clearfix']").addClass(errorClass).removeClass(validClass);
            }, 
            unhighlight: function (element, errorClass, validClass) { 
    $(element).parents(".error").removeClass(errorClass).addClass(validClass); 
            },
            rules: {
                name: {
                    required: true,
                    minlength: 2
                },
                subject: {
                    required: true,
                    minlength: 10
                },
                email: {
                    required: true,
                    email: true
                },
                message: {
                    required: true,
                    minlength: 10
                }
            },
            messages: {
                name: {
                    required: '<span class="help-inline">Your name is required</span>',
                    minlength: jQuery.format('<span class="help-inline">2 chars</span>')
                },
                subject: {
                    required: '<span class="help-inline">A Subject is required.
    </span>',
     minlength:jQuery.format('<span class="help-inline">10 characters</span>')
                },
                email: {
                    required: '<span class="help-inline">Email.</span>',
                    email: '<span class="help-inline">Ex : name@exemple.com</span>'
                },
                message: {
                    required: '<span class="help-block">Message</span>',
                    minlength: jQuery.format('<span class="help-block">10 chars</span>')
                }
            },
            submitHandler: function(form) 
            {
                $('form').submit(function() {
                    if ($validator.numberOfInvalids() > 0) 
                    {
                        $('#submit').modal('hide');
                    } else 
                    {
                        $('#submit').modal('show');
                    }
                });
            }
        });
    });

2 个答案:

答案 0 :(得分:0)

在每个输入字段上添加“required”。确保每个字段都有数据的简单方法

pool = null;
last = 0;
function connectionPool(connectionsNumber) {
  pool = [];
  for (var i=0; i < connectionsNumber; ++i) {
    pool.push(mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : 'Rileysteele1',
        dateStrings : true,
        database : 'twitter',
        port     : 3306
    }));
  }
}

function getConnection() {
  if (!pool) {
    initializeConnection(40);
  }
  var connection = pool[last];
  last++;
  if (last == pool.length)
    last = 0;
  return connection;
}

function initializeConnection() {
  pool = new connectionPool(2);
}

exports.initializeConnection = initializeConnection;
exports.getConnection = getConnection;

答案 1 :(得分:0)

您是否已经包含了jquery和验证库?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>