源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

vue生成随机验证码的示例代码

  • 时间:2020-06-23 06:52 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:vue生成随机验证码的示例代码
本文介绍了vue生成随机验证码的示例代码,分享给大家,具体如下: 样式自调,最终效果如图: [img]http://files.jb51.net/file_images/article/201709/2017929110036642.png?201782911051[/img] 实现效果: 点击右边input框会自动切换,如果输入的值与字不同,则清空换一串随机数 HTML
<input type="text" placeholder="请输入验证码" class="yanzhengma_input" @blur="checkLpicma" v-model="picLyanzhengma">
<input type="button" id="code" @click="createCode" class="verification1" v-model="checkCode"/> <br>
<span class="tishixiaoxi disappear">请输入验证码。</span>
<a class="user_login" @click="Login">登录</a>
JS
// 图片验证码
createCode(){
  code = ""; 
  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');//随机数 
  for(var i = 0; i < codeLength; i++) {
   //循环操作 
   var index = Math.floor(Math.random()*36);//取得随机数的索引(0~35) 
   code += random[index];//根据索引取得随机数加到code上 
  } 
  this.checkCode = code;//把code值赋给验证码 
},
// 失焦验证图和密码
checkLpicma(){   
  this.picLyanzhengma.toUpperCase();//取得输入的验证码并转化为大写   
  if(this.picLyanzhengma == '') {
   //代码是直接复制项目内容,这里选择器选择时 请根据自己的需求来实现提示语效果,很简单,我懒,就不改了 ~
   $(".login_content1 span:eq(2)").text("请输入验证码")
   $(".login_content1 span:eq(2)").removeClass("disappear");  
  }else if(this.picLyanzhengma.toUpperCase() != this.checkCode ) { 
   //若输入的验证码与产生的验证码不一致时 
   console.log( this.picLyanzhengma.toUpperCase())
   //代码是直接复制项目内容,这里选择器选择时 请根据自己的需求来实现提示语效果,很简单,我懒,就不改了 ~  
   $(".login_content1 span:eq(2)").text("验证码不正确")
   $(".login_content1 span:eq(2)").removeClass("disappear");
   this.createCode();//刷新验证码 
   this.picLyanzhengma = '';
  }else {
   //输入正确时 
   //代码是直接复制项目内容,这里选择器选择时 请根据自己的需求来实现提示语效果,很简单,我懒,就不改了 ~  
   $(".login_content1 span:eq(2)").addClass("disappear");
   $(".login_content1 span:eq(2)").text("请输入验证码")
   return true;
  } 
}
友情提示:本文直接从项目拿来供大家思路参考,验证提示那块大家可根据自己情况做更改。懒得改的可以去我github拿demo。 demo已放到鄙人github,也可自取:[url=https://github.com/JOSIE1988/JS-Random-authentication-code]https://github.com/JOSIE1988/JS-Random-authentication-code[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部