JavaScript编写验证码
<head>
<meta charset="UTF-8">
<title>验证码</title>
</head>
<body>
<span id="msg"></span>
<input type="button" value="刷新" οnclick="show()">
<script>
function str(n){
var a = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
var t = ""; //用来保存验证码
for(var i=0;i<n;i++){
//因为可以选取的字母和数字有62个,所以随机选取一个数
var index = Math.floor(Math.random()*62);
//将选取到的这个数当做一个位置,从a中选取这个位置的值
t += a.charAt(index);
}
return t;
}
function show() {
document.getElementById("msg").innerHTML = str(4);
}
window.onload(show());
</script>
</body> 可以根据自己的需要改变成6位/8位等
还没有评论,来说两句吧...