生成指定位数的仅包含数字跟字母的验证码:
generateRandomAlphaNum(len):len为验证码字符个数
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <span id="aiphaNum"></span> <script type="text/javascript"> function generateRandomAlphaNum(len) { var rdmString = ""; for(var i = 0; rdmString.length < len; i++){ rdmString += Math.random().toString(36).substr(2); }; return rdmString.substr(0, len); }; document.getElementById("aiphaNum").innerHTML = generateRandomAlphaNum(4); </script> </body> </html>