Open In App

Create a Password Generator using HTML CSS and jQuery

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
5 Likes
Like
Report

In this article, we will create a password generator using jQuery. With this tool, users can easily customize their password by specifying the desired length and selecting options like­ including symbols, numbers, lowercase, and uppe­rcase letters. Furthermore, the gene­rated password can be convenie­ntly copied to the clipboard for immediate­ use.

Create-Password-Generator-Using-HTML-&-jQuery

Approach

  • We start by setting up the HTML structure­ and implementing CSS styles. This approach grants our ge­nerator a clean and visually appealing design.
  • The inclusion of the jQue­ry library facilitates seamless manipulation of the­ Document Object Model (DOM) and stre­amlines user interactions.
  • The ge­neratePassword function serve­s as a fundamental component within the application. It care­fully considers the user's pre­ferences, taking into account factors such as password le­ngth and options for different character type­s. It generates a random password based on the selected character types and length. The generated password is stored in the password variable.
  • The function known as copyToClipboard provide­s users with the ability to easily duplicate­ the generate­d password into their clipboard.

Example: Below is the implementation of the project.

  JavaScript
$(document).ready(function () {
    let password = '';

    function generatePassword() {
        const passwordLength =
            parseInt($('#passwordLength').val());
        const useSymbols =
            $('#useSymbols').is(':checked');
        const useNumbers =
            $('#useNumbers').is(':checked');
        const useLowerCase =
            $('#useLowerCase').is(':checked');
        const useUpperCase =
            $('#useUpperCase').is(':checked');

        let charset = '';
        let newPassword = '';

        if (useSymbols) charset += "!@#$%^&*()";
        if (useNumbers) charset += "0123456789";
        if (useLowerCase) charset += "abcdefghijklmnopqrstuvwxyz";
        if (useUpperCase) charset += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        for (let i = 0; i < passwordLength; i++) {
            newPassword += charset.charAt(
                Math.floor(Math.random() * charset.length));
        }

        password = newPassword;
    }

    function copyToClipboard() {
        const el = document.createElement('textarea');
        el.value = password;
        document.body.appendChild(el);
        el.select();
        document.execCommand('copy');
        document.body.removeChild(el);
        $('#successMessage').text('Password copied to clipboard!');
        setTimeout(function () {
            $('#successMessage').text('');
        }, 2000);
    }

    $('#generatePassword').click(function () {
        generatePassword();
        $('#generatedPassword').val(password);
        $('#generatedPasswordContainer').show();
    });

    $('#copyToClipboard').click(function () {
        copyToClipboard();
    });
});
HTML CSS

Output:
Create-Password-Generator-Using-HTML-&-jQuery


Next Article

Similar Reads