1000hz bootstrap验证器data-toggle =“验证器”无法正常工作

时间:2016-02-09 09:36:31

标签: javascript jquery twitter-bootstrap validation bootstrapvalidator

我正在尝试使用以下验证器http://1000hz.github.io/bootstrap-validator/验证以下表单我从验证器插件的文档中复制了大部分元素,当我按下提交按钮时,它总是传递出错误。我添加了“data-toggle =”验证器“,它说它应该自动验证。我也尝试使用底部的脚本。有人可以给我看一个如何使用它的示例。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="dist/css/bootstrap.min.css" rel="stylesheet">
  </head>
   <body>
         <script src="js/bootstrap.min.js"></script>
         <script src="dist/js/bootstrap.min.js"></script>
         <script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.9.0/validator.min.js"></script>
         <h1>Register Below! </h1>

      <form data-toggle="validator" role="form" method="post" class="form-horizontal" name="myForm">
            <div class="form-group">
              <label for="inputName" class="col-md-3 control-label">Name</label>
            <div class="col-md-6">
             <input type="text" class="form-control" id="inputName" name="name" placeholder="Username" required />
           </div>
          </div>


           <div class="form-group">
            <label for="inputEmail" class="col-md-3 control-label">Email</label>
           <div class="col-md-6">
            <input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" data-error="Incorrect email address" required>
           <div class="help-block with-errors"></div>
          </div>
          </div>
          <div class="form-group">
            <label for="inputPassword" class="col-md-3 control-label">Password</label>
          <div class="form-group col-sm-6">
            <input type="password" data-minlength="6" class="form-control" id="inputPassword" name="password" placeholder="Password" required>
          <span class="help-block">Minimum of 6 characters</span>
          </div>
          </div>
          <div class="form-group">
            <label for="inputPassword" class="col-md-3 control-label">Confirm Password</label>
          <div class="form-group col-sm-6">
            <input type="password" class="form-control" id="inputPasswordConfirm" data-match="#inputPassword" data-match-error="The password doesnt match" placeholder="Confirm" required>
          <div class="help-block with-errors"></div>
         </div>
        </div>
        <div class="form-group">
        <div class="col-md-9 col-md-offset-3">
          <button type="submit" name="submit" class="btn btn-primary">Register</button>
       </div>
    </div>
   </form>

        <script >
          (document).ready(function() {
           $('#myForm').validator()
             $('#myForm').submit(function (e) {
                 $('#myForm').validator('validate')
              });
           });

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

1 个答案:

答案 0 :(得分:4)

你的方法中的一些错误

  1. 不包含jQuery lib(必须包含在所有其他JS库之上)
  2. bootstrap.min.js包括两次(删除一个)
  3. 在脚本<{li>中的$之前遗失(document).ready(function()
  4. myForm
  5. 中缺少ID选择器<form>

    代码

    没有任何问题

    SideNote:可以使用属性data-toggle="validator"进行验证,也可以通过JS脚本$('#myForm').validator()在表单上进行初始化,而不需要两者。

    Fiddle with data-toggle="validator"

    Fiddle with JS Script