admin管理员组文章数量:1426182
code:
function alphanumeric()
{
var randomnum=Math.random().toString(36).substr(2,8);
document.getElementbyId("textBoxId").value = randomnum;
}
Each time when I call alphanumeric() function this will generate alphanumeric random string and show on the Text Box like.. v3ta4t8l,zshqoc2u,9pey71rb...which works fine.. In my requirement I need to allow only unique alphanumeric. Could you please help me to solve my below question,
1.Is it only generate unique alphanumeric?
2.How many alphanumeric this will generate?
3.Is it possible to allow only unique alphanumeric?
4.It is possible to store unique alphanumeric in database use of primary key in my case already one of my column having primary key and so I will try to implement with the use of java script...
code:
function alphanumeric()
{
var randomnum=Math.random().toString(36).substr(2,8);
document.getElementbyId("textBoxId").value = randomnum;
}
Each time when I call alphanumeric() function this will generate alphanumeric random string and show on the Text Box like.. v3ta4t8l,zshqoc2u,9pey71rb...which works fine.. In my requirement I need to allow only unique alphanumeric. Could you please help me to solve my below question,
1.Is it only generate unique alphanumeric?
2.How many alphanumeric this will generate?
3.Is it possible to allow only unique alphanumeric?
4.It is possible to store unique alphanumeric in database use of primary key in my case already one of my column having primary key and so I will try to implement with the use of java script...
Share Improve this question asked May 22, 2014 at 5:11 vickyvicky 271 gold badge2 silver badges7 bronze badges 5- Have you ever heard of GUIDs? – Matt Ball Commented May 22, 2014 at 5:14
- sorry..I do not have enough idea about that... – vicky Commented May 22, 2014 at 5:29
-
I have looked at the answers and your question and I'm not sure what exactly you mean by unique. Do you want no repeated characters within one string or do you want to emit a series of unique strings when you call
alphanumeric()
repeatedly? – M Oehm Commented May 22, 2014 at 5:29 - I don't need to show any repeated same value...sorry for the inconvenient.. – vicky Commented May 22, 2014 at 5:42
- Related: how to generate uuid in javascript – Ja͢ck Commented May 22, 2014 at 5:48
2 Answers
Reset to default 1Here's something that would return only unique alphanumerics
function alphanumeric_unique() {
return Math.random().toString(36).split('').filter( function(value, index, self) {
return self.indexOf(value) === index;
}).join('').substr(2,8);
}
FIDDLE
Splitting the string into an array of characters, then using Array.filter()
to filter out any characters that are already in the array to get only one instance of each character, and then finally joining the characters back to a string, and running substr(2, 8)
to get the same length string as in the question, where it starts at the second character and gets a total of eight characters.
function IDGenerate() {
var text = "";
var hdntxt = "";
var captchatext = "";
var possible = "ABCDEFGHIkLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 7; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
document.getElementById("txtboxID").value = text;
return false;
}
本文标签: Generate unique random alphanumeric using javascriptStack Overflow
版权声明:本文标题:Generate unique random alphanumeric using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745463747a2659440.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论