Created
December 1, 2019 06:33
-
-
Save raazon/02e528da98293473a0bc0e8470ca5656 to your computer and use it in GitHub Desktop.
Generate random string/characters in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> | |
<script> | |
function generateRandomString(length) { | |
var result = ''; | |
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@!#'; | |
var charactersLength = characters.length; | |
for ( var i = 0; i < length; i++ ) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
$('.generateRandomString').html(result); | |
} | |
</script> | |
</head> | |
<body> | |
<button type="button" onclick="generateRandomString(200)">If you click on me, I will generate a random string.</button> | |
<p class="generateRandomString"></p> | |
</body> | |
</html> | |
<!-- REFERENCE: https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment