Chciałem stworzyć prosty generator hasła.
Pod spodem są checkboxy, które mają sprawdzać, czy hasło zawiera duże i małe litery, cyfry i znaki specjalne.
Zacząłem pisać kod - dość paskudnie wyglądający, wybaczcie :p - ale nie mam pojęcia, jak stworzyć warunek, czy hasło zawiera wyżej wspomniane ani jak zrobić, żeby przy spełnieniu warunku dany checkbox automatycznie się ,,zaznaczył".
Mój mózg złapał error zaraz po zdecydowaniu, że zapewne za pomocą pętli sprawdzającej tablice i ifa
Na chwilę obecną już napisany fragment kodu wygląda tak:
<script>
function draw(number, smallLetter, bigLetter, specialChar)
{
const myNumber = number[Math.round(Math.random()*number.length-1)];
const mySmallLetter = smallLetter[Math.round(Math.random()*smallLetter.length-1)];
const mySmallLetter2 = smallLetter[Math.round(Math.random()*smallLetter.length-1)];
const mySmallLetter3 = smallLetter[Math.round(Math.random()*smallLetter.length-1)];
const myBigLetter = bigLetter[Math.round(Math.random()*bigLetter.length-1)];
const myBigLetter2 = bigLetter[Math.round(Math.random()*bigLetter.length-1)];
const mySpecialChar = specialChar[Math.round(Math.random()*specialChar.length-1)];
const mySpecialChar2 = specialChar[Math.round(Math.random()*specialChar.length-1)];
const myPassword = mySmallLetter + mySpecialChar + myNumber + mySmallLetter2 + mySpecialChar2 + myBigLetter + myBigLetter2 + mySmallLetter3;
return myPassword;
}
numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
smallLetters = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m"];
bigLetters = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A","S", "D", "F", "G", "H", "J","K", "L", "Z", "X", "C", "V", "B", "N", "M"];
specialChars = ["!", "@", "#", "$", "%", "^", "&", "*", "-", "_", "+", "=", "~", "{", "}", "[", "]", ":", ";", ".", "?"];
document.getElementById('password').value = draw(numbers, smallLetters, bigLetters, specialChars);
</script>