When I click on the generate id button, how can i make it generate a unique id everytime I click on it and have it show up in the textbox next to it? assuming the button id is "generateID" and textbox id is "generateidtxt"? it could be all numbers or mixed in with letters. and maybe 8-10 characters long.. I just need it to be different everytime and never repeats.
Asked
Active
Viewed 1,030 times
0
-
1This should help you out http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript – Smeegs Jun 05 '13 at 19:50
-
What do you mean by unique? How unique? Unique for page, system, entity? World? – Jarek Jun 05 '13 at 19:50
-
@pako unique as in every generated id is different from the next button click and so on. – Cloud Jun 05 '13 at 19:53
-
in that case, you can use a counter – jvilhena Jun 05 '13 at 20:04
3 Answers
0
If you are looking for a .net way, on your button click just do
myTextBox.Text = System.Guid.NewGuid();
NewGuid will generate a new guid for you, which is pretty much guaranteed to be unique for each run. more one guid http://msdn.microsoft.com/en-us/library/system.guid.aspx

Paritosh
- 4,243
- 7
- 47
- 80
-
don't forget the `.ToString()`- I.E `myTextBox.Text = System.Guid.NewGuid().ToString();` – Darren Wainwright Jun 05 '13 at 19:52
0
to generate a unique number id, Date.now()
will help,
to make it text + number, use toString(Math.floor(Math.random() * 22) + 15)
full program:
const generateID = () =>
Date.now().toString(Math.floor(Math.random() * 20) + 17);
const btnGenerate = document.getElementById('generateID');
const generateIDTXT = document.getElementById('generateidtxt');
const copy = document.getElementById('copy');
btnGenerate.addEventListener('click', () => {
generateIDTXT.value = generateID();
});
* {
margin: 0;
padding: 0;
font-family: Arial, Helvetica, sans-serif;
}
button {
background-color: #54fdae;
padding: 5px;
margin: 10px;
border: 0;
border-radius: 3px;
outline: 0;
cursor: pointer;
}
button:hover {
background-color: #32ee9a;
}
input {
border: 0;
border-bottom: 1px solid #000;
padding: 3px;
margin: 10px;
outline: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Generate Random ID</title>
</head>
<body>
<button id="generateID">Generate ID</button>
<input type="text" id="generateidtxt" />
</body>
</html>
note: this is 8 - 10 words numbers / letters and this is basically impossible to generate 2 same ids except if 2 people press the button at the same time AND the random number between 17 - 36 is the same number

garry
- 23
- 3