微信小程序生成随机验证码

wxml文件

<view class='makecode' bindtap='getcode'>

{{code}}

</view>

js文件

data: {

code:"获取验证码"

},

getcode:function(){

this.createCode();

},

createCode() {

var code;

//首先默认code为空字符串

code = '';

//设置长度,这里看需求,我这里设置了4

var codeLength = 4;

//设置随机字符

var random = new Array(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');

//循环codeLength 我设置的4就是循环4次

for (var i = 0; i < codeLength; i++) {

//设置随机数范围,这设置为0 ~ 36

var index = Math.floor(Math.random() * 36);

//字符串拼接 将每次随机的字符 进行拼接

code += random[index];

}

//将拼接好的字符串赋值给展示的code

this.setData({

code: code

})

},

猜你喜欢

转载自blog.csdn.net/qq_37902065/article/details/82251191