调用例子1:
randomString()
输出1:
JopzU1pJ7zu8E0d51JamTfE6IPLOGuvx
调用例子2:
randomString(16) //长度为16位
输出2:
Ee2wH4nOUT1eSeLD
调用例子3:
randomString(16, INCLUDE_UPPERCASE) //只包含大写字母
输出3:
GFOBCQSQYFZXHZVD
调用例子4:
randomString(16, INCLUDE_UPPERCASE + INCLUDE_DIGIT) //只包含大写字母和数字
输出4:
16WPG8XHT8A0WC2Y
const INCLUDE_UPPERCASE = 1,INCLUDE_LOWERCASE = 2,INCLUDE_DIGIT = 4;function randomString(len, include) { len = len || 32;include = include || 7;var $uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var $lowercase = $uppercase.toLowerCase(); var $digit = "0123456789"; var $chars = ""; if (include & INCLUDE_UPPERCASE) $chars += $uppercase; if (include & INCLUDE_LOWERCASE) $chars += $lowercase; if (include & INCLUDE_DIGIT) $chars += $digit; var maxPos = $chars.length; var str = ""; for (var i = 0; i < len; i++) { str += $chars.charAt(Math.floor(Math.random() * maxPos)); } return str;}