The first type:
//code from http://caibaojian.com/js-random-string.htmlfunction makeid(){ var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for( var i=0; i < 5; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text;}The second type: no need to enter a character set
function randomstring(L){ var s= ''; var randomchar=function(){ var n= Math.floor(Math.random()*62); if(n<10) return n; //1-10 if(n<36) return String.fromCharCode(n+55); //AZ return String.fromCharCode(n+61); //az } while(s.length< L) s+= randomchar(); return s;} alert(randomstring(5))
The third type: Supports custom character length and feature character collection
function randomString(len, charSet) { charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var randomString = ''; for (var i = 0; i < len; i++) { var randomPoz = Math.floor(Math.random() * charSet.length); randomString += charSet.substring(randomPoz,randomPoz+1); } return randomString;}Call with default charset [a-zA-Z0-9] or send in your own:
var randomValue = randomString(5);var randomValue = randomString(5, 'PICKCHARSFROMTHISSET');
Demo screenshot
The above is a summary of three methods of creating random strings containing numbers and letters in JavaScript. If you need it, you can refer to it and learn it.