9

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の可視性に従って、パスワードを一致させ、パスワードフィールドを確認する方法を教えてください。

4

2 に答える 2

0

.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>

于 2020-11-08T16:31:58.330 に答える