学习心得|生成随机密码

ASP版的 < %
function makePassword(maxLen) ' 方法一
Dim strNewPass
Dim whatsNext, upper, lower, intCounter
Randomize
For intCounter = 1 To maxLen
whatsNext = Int (( 3 ) * Rnd + 0 )
If whatsNext = 0 Then ' 生成大写字母
' 'character
upper = 90
lower = 65
Elseif whatsNext = 1 then ' 生在数字
upper = 57
lower = 48
else ' 生成小写字母
upper = 122
lower = 97
End If
strNewPass = strNewPass & Chr ( Int ((upper - lower + 1 ) * Rnd + lower))
Next
makePassword = strNewPass
end function




Function RoundStr( str,Num) ' 方法二
s = ""
for i = 1 to Num

Randomize

strLen = Len (str)
t = Round (( RND * (strLen - 1 )) + 1 )
s = s & mid (str,t, 1 )
Next
RoundStr = s

End Function

' response.write makePassword(10)&"
"


tmp = " 0123456789abcdefghijklmnopqrstopwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
' response.writeRoundStr(tmp,10)&"
"

% > JavaScript版的
< script >
function makePassword(maxLen)
{
var strNewPass = "" ;
var whatsNext, upper, lower, intCounter;
for (intCounter = 1 ; intCounter <= maxLen; intCounter ++ )
{
whatsNext = Math.floor( 3 * Math.random());
switch (whatsNext)
{
case 0 : // 生成大写字母
upper = 90 ;
lower = 65 ;
break ;

case 1 : // 生成数字
upper = 57 ;
lower = 48 ;
break ;

case 2 : // 生成小写字母
upper = 122 ;
lower = 97 ;
break ;
}
strNewPass = strNewPass + (String.fromCharCode(Math.floor((upper - lower + 1 ) * Math.random() + lower)));
}
return strNewPass

}
document.write(makePassword( 13 ))
学习心得|生成随机密码
文章图片
< INPUT TYPE ="text" NAME ="Password" >
学习心得|生成随机密码
文章图片
< INPUT TYPE ="button" value ="https://www.it610.com/article/随机生成密码" onClick ="document.getElementById('Password').value=https://www.it610.com/article/(makePassword(13))" >

    推荐阅读