password.pt 2.2 KB
<tal:block tal:define="
    name name|field.name;
    oid oid|field.oid;
    ">
  <div class="input">
    <input
        type="password"
        name="${name}"
        value="${field.widget.redisplay and cstruct or ''}"
         tal:attributes="style style|field.widget.style;
            class string: form-control ${css_class|field.widget.css_class or ''};
        attributes|field.widget.attributes|{};"
        id="${oid}"/>
    <!--?  onkeyup="checkPasswordStrength${oid}();"-->

    <div class="checkbox">
      <label>
        <input type="checkbox" id="view${oid}">
        <span>Show Password</span>
      </label>
    </div>
    <div id="${oid}-password-strength-status"></div>
  </div>
  <style>
    #password-strength-status {
      padding: 5px 10px;
      border-radius: 4px;
      margin-top: 5px;
    }
  </style>
  <script type="text/javascript">
    $('#view${oid}').change(function () {
      if ($(this).prop('checked') == true) {
        $('#${oid}').attr('type', 'text');
      } else {
        $('#${oid}').attr('type', 'password');
      }
    });

    function checkPasswordStrength${oid}() {
      var number = /([0-9])/;
      var alphabets = /([a-zA-Z])/;
      var special_characters = /([~,!,@,#,$,%,^,&,*,-,_,+,=,?,>,<,\),\(,{,},\[,\]])/;
      var password = $('#${oid}').val().trim();
      if (password.length <= 8) {
        $('#${oid}-password-strength-status').removeClass();
        $('#${oid}-password-strength-status').addClass('label label-danger');
        $('#${oid}-password-strength-status').html("Weak (should be atleast 8 characters.)");
      } else {
        if (password.match(number) && password.match(alphabets) && password.match(special_characters)) {
          $('#${oid}-password-strength-status').removeClass();
          $('#${oid}-password-strength-status').addClass('label label-success');
          $('#${oid}-password-strength-status').html("Strong");
        } else {
          $('#${oid}-password-strength-status').removeClass();
          $('#${oid}-password-strength-status').addClass('label label-warning');
          $('#${oid}-password-strength-status').html("Medium (should include alphabets, numbers and special characters.)");
        }
      }
    }
  </script>
</tal:block>