1

我正在制作登录和注册页面。我也在实施表单验证。我想做这样的事情 - 当页面首先加载LOGIN页面时activeREGISTER点击时然后注册页面获取active并显示表单。

我已经尝试过这样做jsfiddlejsfiddle link。我不知道为什么过渡效果不起作用。这只是一个简单的过渡panel-heading

HTML-

<div class="panel-heading">
   <div class="row">
      <div class="col-xs-6">
          <a href="#" class="active" id="login-form-link">LOGIN</a>
      </div>
      <div class="col-xs-6">
          <a href="#" class="" id="register-form-link">REGISTER</a>
      </div>
</div>

JS-

$(document).ready(function() {
 $("#login-form-link").click(function(e) {
 $("#login-form").delay(100).fadeIn(100);
 $("#register-form").fadeOut(100);
 $("#register-form-link").removeClass("active");
 $(this).addClass("active");
 e.preventDefault();
});
$("#register-form-link").click(function(e) {
 $("#register-form").delay(100).fadeIn(100);
 $("#login-form").fadeOut(100);
 $("#login-form-link").removeClass("active");
 $(this).addClass("active");
 e.preventDefault();
 });
});

之后,我将专注于 REGISTER 表单的验证。相信我,注册表会在我的浏览器中打开我不知道为什么注册表不会在 jsfiddle 中打开!

请看它像这样的表格:

在此处输入图像描述

我的问题是验证不起作用,我不知道为什么。我查看了许多 Stack Overflow 链接(这个很有帮助),但现在没有任何帮助。

4

1 回答 1

1

您在 jQuery 代码中有多个语法错误,而且您使用了错误的库,

有问题提供的小提琴使用了 bootstrapValidator 库和 jqueryValidation 库,这两个库与 formValidation 无关

如果您检查了浏览器控制台日志,则有一半的问题,syntax errors例如

  • 缺少多个逗号
  • 错误使用分号
  • 错误使用引号

可以很容易地修复你也可以$(...).formValidation is not a function在控制台日志中找到也意味着库丢失了。

这是工作小提琴

语法错误已修复和正确,并formValidation添加了所需的库

  • 过渡效果工作
  • 验证登记表工作

$(document).ready(function() {

  $("#login-form-link").click(function(e) {
    $("#login-form").delay(100).fadeIn(100);
    $("#register-form").fadeOut(100);
    $("#register-form-link").removeClass("active");
    $(this).addClass("active");
    e.preventDefault();
  });
  $("#register-form-link").click(function(e) {
    $("#register-form").delay(100).fadeIn(100);
    $("#login-form").fadeOut(100);
    $("#login-form-link").removeClass("active");
    $(this).addClass("active");
    e.preventDefault();
  });

  $('#register-form').formValidation({

      feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
      },
      fields: {
        fullname: {
          validators: {
            notEmpty: {
              message: "Please enter this field"
            },
            regexp: {
              regexp: /^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$/,
              message: "full name cannot contain this symbol "
            }
          }
        },
        username: {
          validators: {
            notEmpty: {
              message: "Please enter this field"
            },
            stringLength: {
              min: 5,
              max: 15,
              message: "username must be more than 5 and less than 15 characters long"
            },
            regexp: {
              regexp: /^[a-zA-Z0-9_\.]+$/,
              message: 'username can only consist of alphabets, numbers, dot and underscore'
            }
          }
        },
        email: {
          validators: {
            notEmpty: {
              message: "Please enter this field"
            },
            emailAddress: {
              message: "invalid email!"
            }
          }
        },
        address: {
          validators: {
            notEmpty: {
              message: "Please enter this field"
            },
            regexp: {
              regexp: /^[a-zA-Z0-9\s,\/-]+$/,
              message: "invalid symbol!"
            }
          }
        },
        pincode: {
          validators: {
            notEmpty: {
              message: "Please enter this field"
            },
            stringLength: {
              min: 6,
              max: 6,
              message: "invalid pincode!"
            },
            digits: {
              message: "pin code can contain digits only"
            }
          }
        },
        phnumber: {
          validators: {
            notEmpty: {
              message: "Please enter this field"
            },
            stringLength: {
              min: 10,
              max: 10,
              message: "invalid phone number!"
            },
            digits: {
              message: "phone number can contain digits only"
            }
          }
        },
        password: {
          validators: {
            notEmpty: {
              message: "Please enter this field"
            },
            different: {
              field: 'username',
              message: 'The password cannot be the same as username'
            },
            identical: {
              field: "confirm_password",
              message: 'password and its confirm are not the same'
            }
          }
        },
        confirm_password: {
          validators: {
            notEmpty: {
              message: "Please enter this field"
            },
            identical: {
              field: "password",
              message: 'password and its confirm are not the same'
            }
          }
        }
      }
    })
    .on('success.form.bv', function(e) {

      var $form = $(e.target);
      var bv = $form.data('bootstrapValidator');

      $.post($form.attr('action'), $form.serialize(), function(result) {
        console.log(result);
      }, 'json');
    });
});
body {
  padding-top: 90px;
}

.panel-login {
  border-style: solid;
  border-color: #7B68EE;
  -webkit-box-shadow: 1px 2px 3px 1px rgba(0, 0, 0, 0.2);
  -moz-box-shadow: 1px 2px 3px 1px rgba(0, 0, 0, 0.2);
  box-shadow: 1px 2px 3px 1px rgba(0, 0, 0, 0.2);
}

.panel-login>.panel-heading {
  color: #7B68EE;
  background-color: #fff;
  border-color: #fff;
  text-align: center;
}

.panel-login>.panel-heading a {
  text-decoration: none;
  color: #666;
  font-weight: bold;
  font-size: 15px;
  -webkit-transition: all 0.1s linear;
  -moz-transition: all 0.1s linear;
  transition: all 0.1s linear;
}

.panel-login>.panel-heading a.active {
  color: #7B68EE;
  font-size: 18px;
}

.panel-login>.panel-heading hr {
  margin-top: 10px;
  margin-bottom: 0px;
  clear: both;
  border: 0;
  height: 1px;
  background-image: -webkit-linear-gradient(left, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15), rgba(0, 0, 0, 0));
  background-image: -moz-linear-gradient(left, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15), rgba(0, 0, 0, 0));
  background-image: -ms-linear-gradient(left, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15), rgba(0, 0, 0, 0));
  background-image: -o-linear-gradient(left, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15), rgba(0, 0, 0, 0));
}

.panel-login input[type="text"],
.panel-login input[type="email"],
.panel-login input[type="password"] {
  height: 45px;
  border: 1px solid #ddd;
  font-size: 16px;
  -webkit-transition: all 0.1s linear;
  -moz-transition: all 0.1s linear;
  transition: all 0.1s linear;
}

.panel-login input:hover,
.panel-login input:focus {
  outline: none;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  border-color: #ccc;
}

input[type="submit"].btn-login {
  background-color: #59B2E0;
  outline: none;
  color: #fff;
  font-size: 14px;
  height: auto;
  font-weight: normal;
  padding: 14px 0;
  text-transform: uppercase;
  border-color: #59B2E6;
}

input[type="submit"].btn-login:hover,
input[type="submit"].btn-login:focus {
  color: #fff;
  background-color: #53A3CD;
  border-color: #53A3CD;
}

.forgot-password {
  text-decoration: underline;
  color: #888;
}

.forgot-password:hover,
.forgot-password:focus {
  text-decoration: underline;
  color: #666;
}

input[type="submit"].btn-register {
  background-color: #59B2E0;
  outline: none;
  color: #fff;
  font-size: 14px;
  height: auto;
  font-weight: normal;
  padding: 14px 0;
  text-transform: uppercase;
  border-color: #59B2E6;
}

input[type="submit"].btn-register:hover,
input[type="submit"].btn-register:focus {
  color: #fff;
  background-color: #53A3CD;
  border-color: #53A3CD;
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.formvalidation/0.6.1/js/formValidation.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.formvalidation/0.6.1/js/framework/bootstrap.min.js"></script>
<div class="container" style="margin-bottom: 5%;">
  <div class="row">
    <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-login">
        <div class="panel-heading">
          <div class="row">
            <div class="col-xs-6">
              <a href="#" class="active" id="login-form-link">LOGIN</a>
            </div>
            <div class="col-xs-6">
              <a href="#" class="" id="register-form-link">REGISTER</a>
            </div>
          </div>
          <hr>
        </div>
        <div class="panel-body">
          <div class="row">
            <div class="col-lg-12">
              <form id="login-form" action="/login" method="post" role="form" style="display: block;">
                <div class="form-group">
                  <input type="text" name="username" pattern="^[_A-z0-9]{1,}$" maxlength="15" id="username" tabindex="1" class="form-control" placeholder="username" value="" required>
                  <!--error message like: "username already exist" -->
                  <span><p><!--error message here--></p></span>
                </div>
                <div class="form-group">
                  <input type="password" name="password" data-minlength="6" id="inputPassword" tabindex="1" class="form-control" placeholder="password" value="" required>
                  <!--error message like: "username and password do not match" -->
                  <span><p><!--error message here--></p></span>
                </div>
                <div class="form-group text-center">
                  <input type="checkbox" tabindex="3" name="remember" id="remember">
                  <label for="remember">Remember me</label>
                </div>
                <div class="form-group">
                  <div class="row">
                    <div class="col-sm-6 col-sm-offset-3">
                      <input type="submit" name="login-submit" id="login-submit" tabindex="4" class="form-control btn btn-login" value="Log in">
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <div class="row">
                    <div class="col-lg-12">
                      <div class="text-center">
                        <a href="#" tabindex="5" class="forgot-password">Forgot Password?</a>
                      </div>
                    </div>
                  </div>
                </div>
              </form>
              <form id="register-form" action="/register" method="post" role="form" style="display: none;">
                <!-- username -->
                <div class="form-group">
                  <input type="text" name="username" id="username" tabindex="1" class="form-control" placeholder="username" value="" required>
                </div>
                <!-- email -->
                <div class="form-group">
                  <input type="email" name="email" id="Email" tabindex="1" class="form-control" placeholder="email" value="" data-error="Email address is invalid" required>
                </div>
                <!-- fullname -->
                <div class="form-group">
                  <input type="text" name="fullname" id="Name" tabindex="1" class="form-control" placeholder="full name" value="" required>
                </div>
                <!-- address -->
                <div class="form-group">
                  <input type="text" name="address" id="address" tabindex="1" class="form-control" placeholder="address" value="" required>
                </div>
                <!-- pincode -->
                <div class="form-group">
                  <input type="text" name="pincode" id="pincode" tabindex="1" class="form-control" placeholder="pincode" value="" data-error="Invalid Pin code!" required>
                </div>
                <!-- phone number -->
                <div class="form-group">
                  <input type="text" name="phnumber" id="phnumber" tabindex="1" class="form-control" placeholder="phone number" value="" data-error="Invalid Phone Number!" required>
                </div>
                <!-- password -->
                <div class="form-group">
                  <input type="password" name="password" id="inputPassword" tabindex="1" class="form-control" placeholder="password" value="" required>
                </div>
                <!-- confirm password -->
                <div class="form-group">
                  <input type="password" name="confirm_password" id="confirmPassword" tabindex="1" class="form-control" placeholder="confirm password" value="" required>
                </div>
                <!-- register button -->
                <div class="form-group">
                  <div class="row">
                    <div class="col-sm-6 col-sm-offset-3">
                      <input type="submit" name="register-submit" id="register-submit" tabindex="4" class="form-control btn btn-register" value="Register Now">
                    </div>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

于 2017-06-17T07:38:26.167 回答