Press "Enter" to skip to content

How to Calculate Password Strength in jQuery

Password strength checker easy to use online calculates password strength and represents result value (percentage) as a colored bar.

It was built using the jQuery library and the Bootstrap 4 framework.

How to make use of it:

1. Create an input field to simply accept password strings.

<input type="text" placeholder="Type the password" id="idPassword" autofocus />

2. Create the HTML for the password strength checker app.

<div id="typepass">
  <h4>
  </h4>
  <br>
  <div id="strengthResult">
  </div>
</div>

3. Load jQuery JavaScript library and Bootstrap 4 framework within the doc.

<link rel="stylesheet" href="/path/to/cdn/bootstrap.min.css" />
<script src="/path/to/cdn/jquery.slim.min.js"></script>
<script src="/path/to/cdn/bootstrap.min.js"></script>

4. Feel free to override the default parameters within the main.js.

$(document).ready(function() {

  $('#idPassword').on('keyup', function() {

    let textElement = $(this).val()
    let strength = 0

    //=========== Rules ==================
    $('#typepass').find('h4').html(`Your Password: ${textElement}`)

    if (textElement.length > 0) {
      let sizeElements = textElement.length

      if (sizeElements > 10) {

        strength += 30

      } else {
        let calcMath = (sizeElements * 2)

        strength += calcMath

      }

    }

    let lowerCase = new RegExp(/[a-z]/)
    if (lowerCase.test(textElement)) {
      strength += 16
    }

    let upperCase = new RegExp(/[A-Z]/)
    if (upperCase.test(textElement)) {
      strength += 18
    }

    let regularNumber = new RegExp(/[0-9]/i)
    if (regularNumber.test(textElement)) {
      strength += 16
    }

    let specialChars = new RegExp(/[^a-z0-9]/i)
    if (specialChars.test(textElement)) {
      strength += 20
    }
    //============ End Rules==============

    //====== Results Rendering =====================
    if (strength < 21) {
      //red very weak password
      $('#strengthResult').html(
        `
          <h5>Password Strength:</h5>
          <h5>${strength}% = Very Weak</h5>                
          <div class="progress" style="height: 40px;">
              <div class="progress-bar bg-danger" role="progressbar" style="width: ${strength}%" aria-valuenow="${strength}" aria-valuemin="0" aria-valuemax="100"><strong style="font-size: 30px">${strength}%</strong></div>
          </div>`
      )
    } else
    if (strength > 20 && strength < 41) {
      //orange weak password
      $('#strengthResult').html(
        `
          <h5>Strength Analyses:</h5>
          <h5>${strength}% = Weak</h5>                    
          <div class="progress" style="height: 40px;">
            <div class="progress-bar bg-warning" role="progressbar" style="width: ${strength}%" aria-valuenow="${strength}" aria-valuemin="0" aria-valuemax="100"><strong style="font-size: 30px">${strength}%</strong></div>
          </div>`
      )
    } else
    if (strength > 40 && strength < 61) {
      //medium password
      $('#strengthResult').html(
        `
          <h5>Strength Analyses:</h5>
          <h5>${strength}% = Medium </h5>                    
          <div class="progress" style="height: 40px;">
            <div class="progress-bar bg-secondary" role="progressbar" style="width: ${strength}%" aria-valuenow="${strength}" aria-valuemin="0" aria-valuemax="100"><strong style="font-size: 30px">${strength}%</strong></div>
          </div>`
      )
    } else
    if (strength > 60 && strength < 81) {
      // strong password
      $('#strengthResult').html(
        `
          <h5>Strength Analyses:</h5>
          <h5>${strength}% = Strong </h5>
          <div class="progress" style="height: 40px;">
            <div class="progress-bar bg-info" role="progressbar" style="width: ${strength}%" aria-valuenow="${strength}" aria-valuemin="0" aria-valuemax="100"><strong style="font-size: 30px">${strength}%</strong></div>
          </div>`
      )
    } else {
      //very strong password
      $('#strengthResult').html(
        `
          <h5>Strength Analyses:</h5>
          <h5>${strength}% = Very Strong </h5>
          <div class="progress" style="height: 40px;">
            <div class="progress-bar bg-success" role="progressbar" style="width: ${strength}%" aria-valuenow="${strength}" aria-valuemin="0" aria-valuemax="100"><strong style="font-size: 30px">${strength}%</strong></div>
          </div>`
      )
    }
    //====== Results Rendering =====================            

    //====== Hide the div containing the result ====
    if (strength == 0) {

      $('#typepass').addClass('showHidden')

    } else {

      $('#typepass').removeClass('showHidden')

    }

  });

});

Online Password Strength Checker, password strength plugin/Github


See Demo And Download

Official Website(alexandrepalmag): Click Here

This superior jQuery/javascript plugin is developed by alexandrepalmag. For extra Advanced Usages, please go to the official website.

Be First to Comment

    Leave a Reply

    Your email address will not be published.