<html>
<head>
</head>
<body>
<input type="Textbox" id="pwbx" />
<button OnClick="GetRandom()" type="button">generate</button>
<script>
function getCharacter() {
return Math.floor(Math.random() * 18).toString(18);
}
for( var str="", i=0, l=14+Math.floor(Math.random()*3); i<l; i++) {
str += getCharacter();
}
</script>
</body>
</html>
This gives me a random number under 1, like 0.4567432112 etc.
How would I alter this to give me a string of between 14 and 16 characters, random alphanumerical? So a string of either 14, 15 or 16 characters made up of abcdefgh0123456789?
Thanks
This will return you a string with random length from 14-16
function getRandomString(min, max) {
num= Math.ceil(Math.random()*3)+14;
var text="";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for( var i=0; i < num; i++ )
{
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
}
also refer this, Generate random string/characters in JavaScript
edit:
<html>
<head>
</head>
<body>
<input type="Textbox" id="pwbx" />
<button OnClick="GetRandom()" type="button">generate</button>
<script>
function GetRandom(min, max) {
num= Math.ceil(Math.random()*3)+14;
var text="";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for( var i=0; i < num; i++ )
{
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
document.getElementById("pwbx").value=text ;
}
</script>
</body>
</html>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments