简体   繁体   English

在Reload上输入(HTML)中随机化文本(JS)

[英]Randomize Text (JS) in Input (HTML) on Reload

I want to randomize letters and numbers, something like 我想随机化字母和数字,比如

let r = Math.random().toString(36).substring(7); console.log("random", r);

but insert it in a text input HTML field. 但将其插入文本输入HTML字段。 I have already managed to plug in texts from a list, but I want it completely random and to follow with an @something.com. 我已经设法从列表中插入文本,但我希望它完全随机并跟随@ something.com。

For example, I want the input field, on every refresh to have something like the following: 例如,我希望输入字段在每次刷新时都具有如下内容:

8ut5fgh8@gmail.com 8ut5fgh8@gmail.com

0okmnhy4@gmail.com 0okmnhy4@gmail.com

s5g7j9l0@gmail.com s5g7j9l0@gmail.com

The characters before the @gmail.com should be 8 chars long. @ gmail.com之前的字符应该是8个字符长。

Thanks for your help. 谢谢你的帮助。 This is a snippet of what I have already done" 这是我已经做过的片段“

 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> 

If you figured out how to make a domain part - do the rest by analogy. 如果你弄清楚如何建立一个域名 - 通过类比做其余的事情。

 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> 

you can try this 你可以试试这个

 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"> 

this is another way to do it using arrays and join and assuming you only want small letters in your random emails. 这是另一种使用数组和连接的方法,假设您只想在随机电子邮件中使用小写字母。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM