功能展示:
1.點選按鈕,隨機生成數字+大小寫字母驗證碼
所有程式碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>隨機生成驗證碼</title>
</head>
<style>
/*驗證碼*/
.upload-awrp {
overflow: hidden;
margin: 120px 0;
}
.code {
font-family: Arial;
font-style: italic;
font-size: 30px;
border: 0;
padding: 2px 3px;
letter-spacing: 3px;
font-weight: bolder;
float: left;
cursor: pointer;
width: 150px;
height: 60px;
line-height: 60px;
text-align: center;
vertical-align: middle;
border: 1px solid #6D6D72;
}
</style>
<body>
<!--隨機驗證碼-->
<div id="check-code" style="overflow: hidden;">
<div class="code" id="data_code"></div>
</div>
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
$.fn.code_Obj = function(o) {
var _this = $(this);
var options = {
code_l: o.codeLength,//驗證碼長度
codeChars: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
],
codeColors: ['#f44336', '#009688', '#cddc39', '#03a9f4', '#9c27b0', '#5e4444', '#9ebf9f', '#ffc8c4', '#2b4754', '#b4ced9', '#835f53', '#aa677e'],
code_Init: function() {
var code = "";
var codeColor = "";
var checkCode = _this.find("#data_code");
for(var i = 0; i < this.code_l; i++) {
var charNum = Math.floor(Math.random() * 52);
code += this.codeChars[charNum];
}
for(var i = 0; i < this.codeColors.length; i++) {
var charNum = Math.floor(Math.random() * 12);
codeColor = this.codeColors[charNum];
}
console.log(code);
if(checkCode) {
checkCode.css('color', codeColor);
checkCode.className = "code";
checkCode.text(code);
checkCode.attr('data-value', code);
}
}
};
options.code_Init();//初始化驗證碼
_this.find("#data_code").bind('click', function() {
options.code_Init();
});
};
$('#check-code').code_Obj({
codeLength: 5
});
</script>
</body>
</html>複製程式碼