I was able to do this in C# but can't translate it to Javascript. I found this post "Generate random password string with requirements in javascript" but I can't customize this to my requirements:
A password should be at least 8 chracters in length and maximum of 13 characters and must contain at least one character from each of the following string collections:
string specialCharacters = "~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,";
string numbers = "0123456789";
string smallLetters = "abcdefghijklmnopqrstuvwxyz";
string capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Can you please help? Many thanks!
EDIT:
Here's my code in C#. Sorry, it's a bit lengthy:
private string CreateRandomPassword(int passwordLength)
{
string specialCharacters = "~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,";
string numbers = "0123456789";
string smallLetters = "abcdefghijklmnopqrstuvwxyz";
string capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string allowedChars = "";
char[] chars = new char[passwordLength];
string password = String.Empty;
Random rd = new Random();
int ctr = 0;
int prop = 4;
if(!_password.ContainsSpecialCharacters && !_password.ContainsNumbers && !_password.ContainsSmallLetters && !_password.ContainsCapitalLetters)
return String.Empty;
string sc = "";
string num = "";
string sl = "";
string cl = "";
if(_password.ContainsSpecialCharacters)
{
// Get a special character randomly
rd = new Random();
sc = specialCharacters[rd.Next(0, specialCharacters.Length)].ToString();
allowedChars += specialCharacters;
}
else
{
prop--;
}
if(_password.ContainsNumbers)
{
// Get a random number randomly
rd = new Random();
num = numbers[rd.Next(0, numbers.Length)].ToString();
allowedChars += numbers;
}
else
{
prop--;
}
if(_password.ContainsSmallLetters)
{
// Get a small letter randomly
rd = new Random();
sl = smallLetters[rd.Next(0, smallLetters.Length)].ToString();
allowedChars += smallLetters;
}
else
{
prop--;
}
if(_password.ContainsCapitalLetters)
{
// Get a capital letter randomly
rd = new Random();
cl = capitalLetters[rd.Next(0, capitalLetters.Length)].ToString();
allowedChars += capitalLetters;
}
else
{
prop--;
}
for (; ctr < passwordLength - prop; ctr++)
password += allowedChars[rd.Next(0, allowedChars.Length)];
return password + sc + num + sl + cl;
}