How to use a Custom Function with Character Codes In Javascript

In this approach, we will create a custom function that generates random characters by selecting random character codes from the ranges corresponding to alphabets (both lowercase and uppercase), numbers, and special characters. This method gives us more control over the character selection process.

Example:

In this example, we will define character ranges and use String.fromCharCode() to convert the character codes to characters.

JavaScript
function generateCustomPassword(length) {
    const getRandomChar = () => {
        const charSets = [
            [48, 57],  // Numbers (0-9)
            [65, 90],  // Uppercase letters (A-Z)
            [97, 122], // Lowercase letters (a-z)
            [33, 47],  // Special characters (!"#$%&'()*+,-./)
            [91, 96],  // Special characters ([\]^_`)
            
        ];
        
        // Select a random character set
        const charSet = charSets[Math.floor(Math.random() * charSets.length)];
        // Generate a random character code from the selected set
        const charCode = Math.floor(Math.random() * 
        (charSet[1] - charSet[0] + 1)) + charSet[0];
        // Convert the character code to a character
        return String.fromCharCode(charCode);
    };

    let password = '';
    for (let i = 0; i < length; i++) {
        password += getRandomChar();
    }
    
    return password;
}

console.log(generateCustomPassword(12));

Output
[G,+UP`1C__Q


How to Generate a Random Password using JavaScript ?

This article will show you how to generate a random password that may consist of alphabets, numbers, and special characters. This can be achieved in various ways.

These are the following ways:

Table of Content

  • Using .charAt() method
  • Using .toString() method
  • Using a Custom Function with Character Codes

Similar Reads

Using .charAt() method

Make a string consisting of Alphabets (lowercase and uppercase), Numbers, and Special Characters. We will use Math.random() and Math.floor() methods to generate a number between 0 and l-1 (where l is the length of the string). To get the character of the string of a particular index we can use .charAt() method. This will keep concatenating the random character from the string until the password of the desired length is obtained....

Using .toString() method

In this approach, we will use Math.random() method to generate a number between 0 and 1 and then convert it to base36(which will consist of 0-9 and a-z in lowercase letters).using .toString() method. To remove the leading zero and decimal point slice() method will be used and Math.random().toString(36).slice(2) to generate the password.For uppercase letters use the same method as the .uppercase() method in concatenation with the previous method....

Using a Custom Function with Character Codes

In this approach, we will create a custom function that generates random characters by selecting random character codes from the ranges corresponding to alphabets (both lowercase and uppercase), numbers, and special characters. This method gives us more control over the character selection process....

Contact Us