Open In App

Create a Password Validator using Tailwind CSS

Passwords must be strong so that hackers can not hack them easily. The following example shows how to check the password strength of the user input password in Tailwind CSS and jQuery. We will use the validator module to achieve this functionality. We will call the isStrongPassword function and pass the conditions as its parameters.

Prerequisites

Approach

Example: This example shows the implementation of the above-explained approach.






<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
                                   initial-scale=1.0">
    <title>Password Strength Checker</title>
    <!-- Tailwind CSS -->
    <link href=
          rel="stylesheet">
    <!-- Font Awesome -->
    <link href=
          rel="stylesheet">
</head>
 
<body class="bg-gray-100 flex justify-center items-center h-screen">
    <div class="bg-white shadow-md rounded-md p-4 w-full md:w-1/2">
        <h2 class="text-xl font-bold mb-4 text-center">
              Checking Password Strength in JavaScript
          </h2>
        <div class="mb-4">
            <label for="password" class="block">
                  Enter Password:
              </label>
            <div class="flex items-center">
                <input type="password" class="border border-gray-300
                 form-input flex-1" id="password">
                <button class="ml-2 px-3 py-2 border border-gray-300
                 rounded-md bg-white text-gray-600 " type="button"
                        id="togglePassword">
                    <i class="fas fa-eye"></i>
                </button>
            </div>
        </div>
        <div class="mb-4">
            <ul>
                <li id="minLength"><i class="fas fa-times
                    text-red-500"></i> Minimum 8 characters</li>
                <li id="uppercase"><i class="fas fa-times
                    text-red-500"></i> At least one uppercase letter</li>
                <li id="lowercase"><i class="fas fa-times
                     text-red-500"></i> At least one lowercase letter</li>
                <li id="symbol"><i class="fas fa-times
                     text-red-500"></i> At least one symbol (@$!%*?&)</li>
            </ul>
        </div>
        <span id="errorMessage" class="font-semibold text-red-500"></span>
    </div>
 
    <!-- jQuery -->
    <script src=
      </script>
    <!-- Font Awesome -->
    <script src=
      </script>
 
    <script>
        // Function to toggle password visibility
        $('#togglePassword').click(function () {
            const passwordInput = $('#password');
            const icon = $(this).find('i');
 
            if (passwordInput.attr('type') === 'password') {
                passwordInput.attr('type', 'text');
                icon.removeClass('fa-eye').addClass('fa-eye-slash');
            } else {
                passwordInput.attr('type', 'password');
                icon.removeClass('fa-eye-slash').addClass('fa-eye');
            }
        });
 
        $('#password').on('input', function () {
            const password = $(this).val();
            const strongPasswordRegex =
                  /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
            const errorMessage = $('#errorMessage');
 
            $('#minLength').html(password.length >= 8 ?
                '<i class="fas fa-check text-green-500"></i> Minimum 8 characters' :
                '<i class="fas fa-times text-red-500"></i> Minimum 8 characters');
            $('#uppercase').html(/[A-Z]/.test(password) ?
                '<i class="fas fa-check text-green-500"></i> At least one uppercase letter' :
                '<i class="fas fa-times text-red-500"></i> At least one uppercase letter');
            $('#lowercase').html(/[a-z]/.test(password) ?
                '<i class="fas fa-check text-green-500"></i> At least one lowercase letter' :
                '<i class="fas fa-times text-red-500"></i> At least one lowercase letter');
            $('#symbol').html(/[@$!%*?&]/.test(password) ?
                '<i class="fas fa-check text-green-500"></i> At least one symbol (@$!%*?&)' :
                '<i class="fas fa-times text-red-500"></i> At least one symbol (@$!%*?&)');
 
            if (strongPasswordRegex.test(password)) {
                errorMessage.text('Strong Password').removeClass('text-red-500')
                    .addClass('text-green-500');
            } else {
                errorMessage.text('Weak Password').removeClass('text-green-500')
                    .addClass('text-red-500');
            }
        });
    </script>
</body>
 
</html>

Output:




Article Tags :