'Validate password and confirm password fields using bootstrap validator

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?



Solution 1:[1]

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>

Solution 2:[2]

My solution is to change the class of the 'confirm'-input from 'form-control' to 'my-form-control', remove the attribute 'required' from it and add a new attribute 'partner="x" to the initial- as well as the confirm-input.
I adjusted the css and added some code to the standard self-executing form validation js (see the example below as well as the 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>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Harald Wirth