我想将字母和数字随机化,例如
let r = Math.random().toString(36).substring(7);
console.log("random", r);
但是将其插入文本输入HTML字段中。我已经设法从列表中插入文本,但是我希望它是完全随机的,并跟随一个@ something.com。
例如,我希望输入字段在每次刷新时都具有以下内容:
8ut5fgh8@gmail.com
0okmnhy4@gmail.com
s5g7j9l0@gmail.com
@ gmail.com之前的字符应为8个字符。
感谢您的帮助。这是我已经完成的工作的摘要”
var texts = [
"@gmail.com",
"@yahoo.com",
"@xdxd.com"
];
document.getElementById('email0').value = texts[Math.floor(Math.random()*texts.length)];
E-mail: </br><input type="text" id="email0" name="email"><br>
答案 0 :(得分:2)
如果您想出如何制作域的一部分,请类比其余部分。
var texts = [
"@gmail.com",
"@yahoo.com",
"@xdxd.com"
];
var chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
function randomEmail(length) {
let result = ''
for (let i = 0; i <= length; i++) {
result += chars[Math.floor(Math.random() * chars.length)]
}
return result + texts[Math.floor(Math.random() * texts.length)]
}
document.getElementById('email0').value = randomEmail(8)
E-mail: <br><input type="text" id="email0" name="email"><br>
答案 1 :(得分:2)
您可以尝试
var texts = [
"@gmail.com",
"@yahoo.com",
"@xdxd.com"
];
function random() {
var text = "";
var ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 8; i++)
text += ch.charAt(Math.floor(Math.random() * ch.length));
return text;
}
let str = random()+texts[Math.floor(Math.random()*texts.length)];
document.getElementById('email0').value = str
<input id="email0">
答案 2 :(得分:2)
这是使用数组和联接并假设您只希望随机电子邮件中只包含小写字母的另一种方法。
let texts = [
"@gmail.com",
"@yahoo.com",
"@xdxd.com"
];
let alphnum = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
let randomChoice = [texts[Math.floor(Math.random() * texts.length)]]
for (let index = 0; index < 8; index++) {
const element = alphnum[Math.floor(Math.random() * alphnum.length)];
randomChoice.unshift(element)
}
let randomChoiceString = randomChoice.join("")
document.getElementById('email0').value = randomChoiceString