O primeiro tipo:
// código de http://caibaojian.com/js-random-tring.htmlfunction makeId () {var text = ""; var possível = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i <5; i ++) text+= possível.charat (math.floor (math.random () * possível.length)); Retornar texto;}O segundo tipo: sem necessidade de inserir um conjunto de caracteres
função RandomString (l) {var s = ''; var aleatória = function () {var n = math.floor (math.random ()*62); if (n <10) retornar n; // 1-10 if (n <36) retorna string.FromCharCode (n+55); // AZ Return String.FromCharcode (n+61); // az} while (s.length <l) s+= aleatomchar (); retorno s;} alert(randomstring(5))
O terceiro tipo: suporta comprimento personalizado de caracteres e coleção de caracteres de recursos
Função RandomString (Len, Charset) {charset = charset || 'Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789'; var aleatomstring = ''; for (var i = 0; i <len; i ++) {var Randompoz = math.floor (math.random () * charset.length); RandomString += charset.substring (Randompoz, Randompoz +1); } retornar aleatório;}Ligue com o Charset padrão [A-ZA-Z0-9] ou envie por conta própria:
var aleatória = RandomString (5); var aleatória = RandomString (5, 'pickcharsfromthisset');
Captura de tela de demonstração
O exposto acima é um resumo de três métodos de criação de seqüências aleatórias contendo números e letras no JavaScript. Se você precisar, você pode consultar e aprender.