The content of this article is about the WeChat applet example: implementing random verification code (with code), which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
There are often some registration and application pages on mini programs that require a random verification code. I won’t say much about the specific implementation method, just go to the code
<view class='yanzhengma'> <text class='left'>{{code}}</text> <text class='right' bindtap='huanyizhang'>换一张</text> </view>
.yanzhengma { height: 100rpx; display: flex; align-items: center; justify-content: center; } .yanzhengma .left { font-family: Arial; font-style: italic; font-weight: bold; border: 0; letter-spacing: 3px; font-size: 18px; background-color: #ccc; padding: 10rpx; margin-right: 20rpx; color: blue; }
Page({ data: { }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { //刚进入页面随机先获取一个 this.createCode() }, huanyizhang(){ 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 }) }, })
Related recommendations:
WeChat applet example: How to count down the verification code (code)
WeChat Mini Program Example: How to obtain and render data (with code)
The above is the detailed content of WeChat applet example: implementing random verification code (with code). For more information, please follow other related articles on the PHP Chinese website!