Skip to main content
Participant
June 14, 2021
Answered

Autogenerate Password (random string)

  • June 14, 2021
  • 1 reply
  • 1378 views

Hello,

When opening a document, I would like to automatically fill a text field with random text (password generator).
Unfortunately I don't find much about it and my approach seems completely wrong 😞

I hope someone can help me out here 🙂


Here is my code:

 

 

function start_kundenkennwort_gen() {

//var randomstring = this.getField("kd_kundenkennwort");

var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}

document.randform.kd_kundenkennwort.value = randomstring;
}

 

 

This topic has been closed for replies.
Correct answer try67

Can you adapt the script so that it only generates the password once per document?


Sure. Change the last line to:

 

if (this.getField("kd_kundenkennwort").valueAsString=="")

     this.getField("kd_kundenkennwort").value = randomstring;

1 reply

try67
Community Expert
Community Expert
June 14, 2021

Use this code:

 

var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i=0; i<string_length; i++) {
	var rnum = Math.floor(Math.random() * chars.length);
	randomstring += chars.substring(rnum,rnum+1);
}
this.getField("kd_kundenkennwort").value = randomstring;
Participant
June 14, 2021

@try67 you are my hero. It works - thank you so much.

Participant
June 14, 2021

Can you adapt the script so that it only generates the password once per document?