I have stuck with adding and removing the bootstrap validator class through Jquery. I am adding validation if div is visible and remove if div is hidden. Here is my try:
<!--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 Code:
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);
}
}
Validation is working if div gets visible or hidden. But confirm password is not matching data with the password field.
Please help me with how I can match the password and confirm password fields as per the visibility of div?
You could achieve this by toggling .display-none
class as in the below example.
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/[email protected]/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/[email protected]/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>