使用引导验证器验证密码并确认密码字段
Posted
技术标签:
【中文标题】使用引导验证器验证密码并确认密码字段【英文标题】:Validate password and confirm password fields using bootstrap validator 【发布时间】:2015-12-11 09:18:18 【问题描述】:我一直坚持通过 Jquery 添加和删除引导验证器类。如果 div 可见,我将添加验证,如果 div 隐藏,则删除。这是我的尝试:
<!--hidden form field-->
<div class="form-group">
<a href="javascript:validateField();" class="theme-color accountFormToggleBtn display-block">click here to change your password</a>
<div class="accountFormToggle display-none" id="passwordForm">
<div class="col-md-5">
<label for="password">Password</label>
<input type='password' id="password" placeholder="Password" name='pass' class="form-control" value='' data-bv-excluded="false" required>
</div>
<div class="col-md-5 col-md-offset-1">
<label for="exampleInputEmail1">Confirm password</label>
<input type='password' id="password2" placeholder="Confirm password" name='password2' class="form-control" value='' data-bv-excluded="false" data-match="#password" required>
</div>
</div>
</div>
JS代码:
function validateField()
if($('#passwordForm').is(':visible'))
$("#password").attr('data-bv-excluded',true);
$("#password2").attr('data-bv-excluded',true);
else
$("#password").attr('data-bv-excluded',false);
$("#password2").attr('data-bv-excluded',false);
如果 div 可见或隐藏,则验证工作。但是确认密码与密码字段的数据不匹配。
请帮助我如何根据 div 的可见性匹配密码并确认密码字段?
【问题讨论】:
@Epodax:如果 php 人员遇到类似问题,我相信他们也可以指导我。没有别的了。 请不要标记与您的问题无关的事物,标记事物是因为您认为该组可以提供帮助不是它是如何工作的. 仅标记与您的问题相关的内容。 我删除了 php 标签,否则你可以添加 asp.net mvc、spring 和很多其他标签。 您能进一步解释一下Please help me how i can match the password and confirm password fields as per visibility of div?
吗?预期的行为是什么,打印的是什么?你能创建一个codepen或类似的吗?
【参考方案1】:
您可以通过切换.display-none
类来实现这一点,如下例所示。
function validateField()
let cont = $('#passwordForm');
if (cont.hasClass('display-none'))
cont.removeClass('display-none');
$("#password").attr('data-bv-validatorname', true);
$("#password2").attr('data-bv-validatorname', true);
else
cont.addClass('display-none');
$("#password").attr('data-bv-validatorname', false);
$("#password2").attr('data-bv-validatorname', false);
console.log(`$("#password").attr('data-bv-validatorname') is $$("#password2").attr('data-bv-validatorname')`);
console.log(`$("#password2").attr('data-bv-validatorname') is $$("#password2").attr('data-bv-validatorname')`);
$('#passwordForm').bootstrapValidator(
message: 'This value is not valid',
live: 'enabled',
fields:
password:
message: 'The password is not valid',
validators:
notEmpty:
message: 'The password is required and cannot be empty'
,
identical:
field: 'password',
message: 'The passwords must match. '
,
stringLength:
min: 6,
max: 30,
message: 'The password must be more than 6 and less than 30 characters long'
,
regexp:
regexp: /^[a-zA-Z0-9_]+$/,
message: 'The password can only consist of alphabetical, number and underscore'
,
password2:
message: 'The password2 is not valid',
validators:
notEmpty:
message: 'The password2 is required and cannot be empty'
,
identical:
field: 'password',
message: 'The passwords must match. '
,
stringLength:
min: 6,
max: 30,
message: 'The password2 must be more than 6 and less than 30 characters long'
,
regexp:
regexp: /^[a-zA-Z0-9_]+$/,
message: 'The password2 can only consist of alphabetical, number and underscore'
);
$('a.accountFormToggleBtn').on('click', function()
validateField();
);
.display-none
display: none;
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<link rel="stylesheet" href="//cdn.jsdelivr.net/jquery.bootstrapvalidator/0.5.2/css/bootstrapValidator.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.min.js"></script>
<!--hidden form field-->
<div class="form-group">
<a class="theme-color accountFormToggleBtn display-block">click here to change your password</a>
<div class="accountFormToggle display-none" id="passwordForm">
<div class="col-md-5">
<label for="password">Password</label>
<input type='password' id="password" data-bv-identical placeholder="Password" name="password" class="form-control" value="" data-bv-notemty data-bv-excluded="false" required>
</div>
<div class="col-md-5 col-md-offset-1">
<label for="exampleInputEmail1">Confirm password</label>
<input type='password' id="password2" placeholder="Confirm password" name="password2" class="form-control" value="" data-bv-notemty data-bv-excluded="false" data-bv-identical required>
<button class="btn btn-default" id="save">Save password</button>
</div>
</div>
</div>
【讨论】:
【参考方案2】:我的解决方案是将“确认”输入的类从“表单控制”更改为“我的表单控制”,从中删除“必需”属性并添加一个新属性“伙伴=”x " 到初始输入和确认输入。 我调整了css并在标准的自执行表单验证js中添加了一些代码(参见下面的示例以及https://jsfiddle.net/HaraldWirth/Loenyxvs/2/)
(function()
'use strict';
window.addEventListener('load', function()
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Fetch the partner-IDs of the inputs to be compared against each other
var ids = jQuery.unique($('[partner]').map(function()
return $(this).attr('partner');
).get());
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form)
form.addEventListener('submit', function(event)
var i;
var c = false;
// Handle the initial form
for (i = 0; i < ids.length; ++i)
var p = $('[partner=' + ids[i] + ']');
if ((p[0].value !== '' && p[1].value === '') ||
(p[0].value === '' && p[1].value !== '') ||
(p[0].value === '' && p[1].value === '') ||
(p[0].value !== p[1].value))
p[1].classList.remove('match');
p[1].classList.add('no-match');
c = false;
else
p[1].classList.remove('no-match');
p[1].classList.add('match');
c = true;
// Handle changes in the form
$('[partner]').on('keyup', function()
for (i = 0; i < ids.length; ++i)
var p = $('[partner=' + ids[i] + ']');
c = false;
if ((p[0].value !== '' && p[1].value === '') ||
(p[0].value === '' && p[1].value !== '') ||
(p[0].value === '' && p[1].value === '') ||
(p[0].value !== p[1].value))
c = false;
p[1].classList.remove('match');
p[1].classList.add('no-match');
else
c = true;
p[1].classList.remove('no-match');
p[1].classList.add('match');
);
if (form.checkValidity() === false || c === false)
event.preventDefault();
event.stopPropagation();
form.classList.add('was-compared');
form.classList.add('was-validated');
, false);
);
, false);
)();
.form-wrapper
padding: 20px;
background-color: #f4f4f4;
border-radius: 15px;
height: 100%;
.btn.btn-submit
background-color: #007093;
border-color: #007093;
padding-left: 20px;
padding-right: 20px;
padding-top: 4px;
padding-bottom: 4px;
margin-top: 20px;
margin-bottom: 20px;
color: #fff;
.my-form-control
display: block;
width: 100%;
height: calc(1.5em + .75rem + 2px);
padding: .375rem .75rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #495057;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: .25rem;
transition: border-color .15s ease-in-out, box-shadow .15s ease-in-out;
.my-form-control.no-match:focus,
.was-compared .my-form-control.no-match:focus
border-color: #dc3545;
box-shadow: 0 0 0 0.2rem rgb(220 53 69 / 25%);
.my-form-control.no-match,
.was-compared .my-form-control.no-match
border-color: #dc3545;
padding-right: calc(1.5em + .75rem);
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23dc3545' viewBox='-2 -2 7 7'%3e%3cpath stroke='%23dc3545' d='M0 0l3 3m0-3L0 3'/%3e%3ccircle r='.5'/%3e%3ccircle cx='3' r='.5'/%3e%3ccircle cy='3' r='.5'/%3e%3ccircle cx='3' cy='3' r='.5'/%3e%3c/svg%3E");
background-repeat: no-repeat;
background-position: center right calc(.375em + .1875rem);
background-size: calc(.75em + .375rem) calc(.75em + .375rem);
.my-form-control:focus
color: #495057;
background-color: #fff;
border-color: #80bdff;
outline: 0;
box-shadow: 0 0 0 0.2rem rgb(0 123 255 / 25%);
.my-form-control.match:focus,
.was-compared .my-form-control.match:focus
border-color: #28a745;
box-shadow: 0 0 0 0.2rem rgb(40 167 69 / 25%);
.my-form-control.match,
.was-compared .my-form-control.match
border-color: #28a745;
padding-right: calc(1.5em + .75rem);
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3e%3cpath fill='%2328a745' d='M2.3 6.73L.6 4.53c-.4-1.04.46-1.4 1.1-.8l1.1 1.4 3.4-3.8c.6-.63 1.6-.27 1.2.7l-4 4.6c-.43.5-.8.4-1.1.1z'/%3e%3c/svg%3e");
background-repeat: no-repeat;
background-position: center right calc(.375em + .1875rem);
background-size: calc(.75em + .375rem) calc(.75em + .375rem);
.my-form-control:focus
color: #495057;
background-color: #fff;
border-color: #80bdff;
outline: 0;
box-shadow: 0 0 0 0.2rem rgb(0 123 255 / 25%);
.my-form-control.no-match~.invalid-feedback,
.my-form-control.no-match~.invalid-tooltip,
.was-compared .my-form-control.no-match~.invalid-feedback,
.was-compared .my-form-control.no-match~.invalid-tooltip
display: block;
.invalid-feedback
display: none;
width: 100%;
margin-top: .25rem;
font-size: 80%;
color: #dc3545;
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js"></script>
<div class="form-wrapper">
<form id="my-form" class="needs-validation" name="my-form" action="xxx.php" method="POST" novalidate>
<div class="messages"></div>
<div class="controls">
<div class="form-row">
<div class="col-md-6 order-md-1">
<div class="form-group">
<label>
<span>First name:</span>
<span class="must">*</span>
</label>
<input class="form-control firstname" type="text" id="firstname" name="firstname" required data-error="Please enter your first name." />
<div class="invalid-feedback">Please enter your first name.</div>
</div>
</div>
<div class="col-md-6 order-md-2">
<div class="form-group">
<label>
<span>Last name:</span>
<span class="must">*</span>
</label>
<input class="form-control lastname" type="text" id="lastname" name="lastname" required data-error="Please enter your last name." />
<div class="invalid-feedback">Please enter your last name.</div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 order-md-1">
<div class="form-group">
<label>
<span>Email address:</span>
<span class="must">*</span>
</label>
<input class="form-control email" partner="0" type="email" id="email" name="email" required data-error="Please enter a valid email address." />
<div class="invalid-feedback">Please enter a valid email address.</div>
</div>
</div>
<div class="col-md-6 order-md-2">
<div class="form-group">
<label>
<span>Confirm email address:</span>
<span class="must">*</span>
</label>
<input class="my-form-control confirm-email" partner="0" type="email" id="confirm-email" name="confirm-email" data-error="The email addresses do not match." />
<div class="invalid-feedback">The email addresses do not match.</div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 order-md-1">
<div class="form-group">
<label>
<span>Password:</span>
<span class="must">*</span>
</label>
<input class="form-control passwd" partner="a" type="password" id="passwd" name="passwd" pattern="^(?=.*?[A-Z])(?=(.*[a-z])1,)(?=(.*[\d])1,)(?=(.*[\W])1,)(?!.*\s).8,$" required data-error="Min. 8 digits: capital & small letters, numbers and signs."
/>
<div class="invalid-feedback">Min. 8 digits: capital & small letters, numbers and signs.</div>
</div>
</div>
<div class="col-md-6 order-md-2">
<div class="form-group">
<label>
<span>Confirm password:</span>
<span class="must">*</span>
</label>
<input class="my-form-control confirm-passwd" partner="a" type="password" id="confirm-passwd" name="confirm-passwd" data-error="The passwords do not match." />
<div class="invalid-feedback">The passwords do not match.</div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12 order-1">
<div class="form-group">
<div class="text-center">
<p>A good password has at least 8 digits and must contain capital & small letters, numbers and signs.</p>
<button class="btn btn-submit" id="btn-submit" type="submit">SUBMIT</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
【讨论】:
以上是关于使用引导验证器验证密码并确认密码字段的主要内容,如果未能解决你的问题,请参考以下文章
使用 parsley js 进行表单验证。字母数字和密码确认
在 Angular 6 中创建用于跨字段验证(确认密码)的自定义指令