Search code examples
javascriptjquerytwitter-bootstrapvalidationbootstrapvalidator

1000hz bootstrap validator data-toggle="validator" not working


I am trying to use the following validator http://1000hz.github.io/bootstrap-validator/ to validate a below form I copied most of the elements from the documentation of the validator plugin and when I press the submit button it always passes with out error.I added "data-toggle="validator" and it says it should automatically validateit.and i also tried to use the script at the bottom. can some one please show me a sample how to use it.

<!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>

Solution

  • Couple of mistakes in your approach

    1. No jQuery lib included (must be included above all other JS libraries)
    2. bootstrap.min.js included twice (remove one)
    3. missing $ before (document).ready(function() in script
    4. missing id selector myForm in <form>

    Rest nothing is wrong with the code

    SideNote: validation can be done either with attribute data-toggle="validator" OR initialize on form via JS script $('#myForm').validator(), no need of both.

    Fiddle with data-toggle="validator"

    Fiddle with JS Script