Home > Enterprise >  Password Checking/ Strengthening Password
Password Checking/ Strengthening Password

Time:12-02

I'm trying to create an account for a user and I want to strengthen the password, what should I add?

I don't know to strengthen the password, by this I meant that I want to require Upper case and lower case as well as adding numbers to a password.

am I missing something? or something is wrong here? Should I change everything here?

<div >
  <label >Username</label>
  <div >
  <input data-parsley-type="alphanum" type="text" name="username" pattern="(?=.*[a-z]).{10, }. " required="" placeholder="" >
   </div>
</div>
<div >
  <label  for="pass1">Password</label>
  <div >
  <input  data-parsley-type="alphanum" type="passwrd" id="password" name="password"  required="" placeholder=""  onkeyup="return validate()">
    <ul>
        <li id="uppercase">At least 1 Uppercase Character</li>
        <li id="lowercase">At least 1 Lowercase Character</li>
        <li id="num">At least 1 Number</li>
        <li id="maxChar">At least 8 characters long</li>
    </ul>
   </div>
</div>

tried this for the js I think I messed everything up here since it is not working

$(document).ready(function(){
            var lowercase = new RegExp('[a-z]');
            var uppercase = new RegExp('[A-Z]');
            var number = new RegExp('[0-9]');
            $("#password").keyup(function(){
                var password = $(this).val()
                    if(password.length == 8){
                        $("maxChar").css("color", "green");
                    }else{
                        $("maxChar").css("color", "red");
                    }
                    if(password.match(lowercase)){
                        $(".lowercase").css("color", "green");
                    }else{
                        $(".lowercase").css("color", "red");
                    }
                    if(password.match(uppercase)){
                        $(".uppercase").css("color", "green");
                    }else{
                        $(".uppercase").css("color", "red");
                    }
                    if(password.match(number)){
                        $(".num").css("color", "green");
                    }else{
                        $(".num").css("color", "red");
                    }
                    if (/^[a-zA-Z0-9]=$/.test(password) == false){

                    }   $(".symbol").css("color", "green");
                    }else{
                        $(".symbol").css("color", "red");
                    }
            })
    </script>-->

CodePudding user response:

There are issues with your code:

  • it produces syntax error: excess {, and missing })
  • the referenced elements are off: class vs id
  • logical error: test for >=
  • regex error for symbol
  • onkeyup="" not needed when using jQuery

Fixed code:

$(document).ready(function() {
  $("#password").keyup(function() {
    var password = $(this).val();
    if(password.length >= 8){
      $("#maxChar").css("color", "green");
    } else {
      $("#maxChar").css("color", "red");
    }
    if(/[a-z]/.test(password)) {
      $("#lowercase").css("color", "green");
    } else {
      $("#lowercase").css("color", "red");
    }
    if(/[A-Z]/.test(password)) {
      $("#uppercase").css("color", "green");
    } else {
      $("#uppercase").css("color", "red");
    }
    if(/[0-9]/.test(password)) {
      $("#num").css("color", "green");
    } else {
      $("#num").css("color", "red");
    }
    if(/[^a-zA-Z0-9]/.test(password)) {
      $("#symbol").css("color", "green");
    } else {
      $("#symbol").css("color", "red");
    }
  }).trigger('keyup');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div >
  <label >Username</label>
  <div >
  <input data-parsley-type="alphanum" type="text" name="username" pattern="(?=.*[a-z]).{10, }. " required="" placeholder=""  />
   </div>
</div>
<div >
  <label  for="pass1">Password</label>
  <div >
  <input  data-parsley-type="alphanum" type="password" id="password" name="password"  required="" placeholder=""  />
    <ul>
        <li id="uppercase">At least 1 Uppercase Character</li>
        <li id="lowercase">At least 1 Lowercase Character</li>
        <li id="symbol">At least 1 Symbol</li>
        <li id="num">At least 1 Number</li>
        <li id="maxChar">At least 8 characters long</li>
    </ul>
   </div>
</div>

  • Related