How to validate password field in bootstrap?

2019-06-09 09:07发布

I am working in asp.net with bootstrap. I want to validate password field. My code display info box when the user click on password field.

<div id="pswd_info">
  <h4>Password requirements:</h4>
     <ul>
       <li id="letter" class="invalid fa-warning"> At least <strong>one letter</strong></li>
       <li id="capital" class="invalid"> At least <strong>one capital letter</strong></li>
       <li id="number" class="invalid"> At least <strong>one number</strong></li>
       <li id="special" class="invalid"> At least <strong>one special character</strong></li>
       <li id="length" class="invalid"> Be at least <strong>8 characters</strong></li>
     </ul>
   </div>

javascript

 <!-- password info box-->
    <script>
        $(document).ready(function () {

            $('#password').keyup(function () {

                // set password variable
                var pswd = $(this).val();

                //validate the length
                if (pswd.length < 8) {
                    $('#length').removeClass('valid').addClass('invalid');
                } else {
                    $('#length').removeClass('invalid').addClass('valid');
                }

                //validate letter
                if (pswd.match(/[A-z]/)) {
                    $('#letter').removeClass('invalid').addClass('valid');
                } else {
                    $('#letter').removeClass('valid').addClass('invalid');
                }

                //validate capital letter
                if (pswd.match(/[A-Z]/)) {
                    $('#capital').removeClass('invalid').addClass('valid');
                } else {
                    $('#capital').removeClass('valid').addClass('invalid');
                }

                //validate number
                if (pswd.match(/\d/)) {
                    $('#number').removeClass('invalid').addClass('valid');
                } else {
                    $('#number').removeClass('valid').addClass('invalid');
                }

                //validate special character
                if (pswd.match(/[!@#$ %^&*]/)){
                    $('#special').removeClass('invalid').addClass('valid');
                } else {
                    $('#special').removeClass('valid').addClass('invalid');
                }

            });
            $('#password').focus(function () {
                $('#pswd_info').show();
            });
            $('#password').blur(function () {
                $('#pswd_info').hide();
            });

        });
    </script>

The above code just display the info box and change each li depend on user entry. But, still accept any password that does not meet the criteria. How can I validate the #password field? I already have the below script that validate other fields

JavaScript

 <script>
      // initialize the validator function
      validator.message.date = 'not a real date';
      // validate a field on "blur" event, a 'select' on 'change' event & a '.reuired' classed multifield on 'keyup':
      $('form')
        .on('blur', 'input[required], input.optional, select.required', validator.checkField)
        .on('change', 'select.required', validator.checkField)
        .on('keypress', 'input[required][pattern]', validator.keypress);
      $('.multi.required').on('keyup blur', 'input', function() {
        validator.checkField.apply($(this).siblings().last()[0]);
      });
      $('form').submit(function(e) {
        e.preventDefault();
        var submit = true;
        // evaluate the form using generic validaing
        if (!validator.checkAll($(this))) {
          submit = false;
        }
        if (submit)
          this.submit();
        return false;
      });
    </script>

1条回答
走好不送
2楼-- · 2019-06-09 09:35

Minimum eight characters, at least one letter and one number:

"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"

Minimum eight characters, at least one letter, one number and one special character:

"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$"

Minimum eight characters, at least one uppercase letter, one lowercase letter and one number:

"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$"

Minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character:

"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}"

Minimum eight and maximum 10 characters, at least one uppercase letter, one lowercase letter, one number and one special character:

"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}"
查看更多
登录 后发表回答