gpt4 book ai didi

javascript - Bootstrap 中的 FormValidation 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 15:47:57 25 4
gpt4 key购买 nike

我正在制作一个登录和注册页面。我正在实现 form validation还。我想做这样的事情 - 当页面首先加载时,LOGIN 页面将变得active,当单击 REGISTER 时,注册页面将变得active 并显示表单。

我尝试通过 jsfiddle 进行操作:jsfiddle link 。不知道为什么过渡效果不起作用。这只是面板标题上的一个简单转换:

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

之后,我将重点关注注册表单的验证。相信我,注册表单会在我的浏览器中打开,我不知道为什么注册表单无法在 jsfiddle 中打开!

请看一下这样的表格:

enter image description here

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

最佳答案

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

fiddle提供的问题中使用的bootstrapValidator库还有jqueryValidation库,这2个库与formValidation无关

如果您检查浏览器控制台日志,一半的问题是语法错误,例如

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

可以轻松修复,您还可以在控制台日志中找到$(...).formValidation 不是函数,这也意味着库丢失。

这是工作fiddle

修复并纠正了语法错误,并添加了必需的 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>

关于javascript - Bootstrap 中的 FormValidation 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43399459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com