随机生成验证码 兼容ie

  • 随机生成验证码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//验证码 
const verifyCode = function (el) {
var arrayTest = ["m", "n", "v", "x", "z", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "q", "w", "r", "t", "y", "u", "i", "i", "o", "p", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

function randomColor() {
var r = parseInt(Math.random() * 256);
var g = parseInt(Math.random() * 256);
var b = parseInt(Math.random() * 256);
var rgb = "rgb(" + r + "," + g + "," + b + ")";
return rgb;
}


function createSpan(code) {
let span = document.createElement('span');
span.style.display = "inline-block"
span.style.height = "100%"
span.style.width = "25%" // 宽度根据验证码个数来定
span.style.color = randomColor()
span.innerHTML = code;
return span
}

const element = document.getElementById(el);
element.innerHTML = ''
let codeArr = []
// 禁用复制
element.oncopy = function () {
return false
}
//表示循环几次,循环出多少个数值.
for (var i = 0; i < 4; i++) {
var num = parseInt(Math.random() * arrayTest.length);
var code = arrayTest[num];
element.style.background = randomColor()
element.appendChild(createSpan(code))
codeArr.push(code)
}
return codeArr.join('')
}
export default verifyCode