登录(实时验证手机格式、验证码)

迷南。 2021-07-24 19:21 584阅读 0赞
  1. 思路:
  2. 动态验证手机号格式:
  3. 利用计算属性会根据内容的变化而调用的特点,将表单绑定v-model,在
  4. 计算属性中,利用正则不断检验手机格式,正确返回true
  5. 验证码:
  6. 当手机号格式正确时,点击才有效,利用时间制造三元运算符改变验证码的文本内容,且倒计时期间,再次点击无效

代码示例:

  1. <template>
  2. <div class='login'>
  3. <p @click='$router.back()'><</p>
  4. <div class='md'>
  5. <p>Jeff外卖</p>
  6. <div class='linfo'>
  7. //验证手机号
  8. <div v-show='flag'>
  9. <div class='mp'>
  10. <span>手机号</span>
  11. <input type="text" placeholder="" v-model="phone">
  12. <span :class='{rphone:right_phone}' @click='getNum'>{ { time?'剩下'+time+'秒':'获取验证码'}}</span>
  13. //利用time是否为0动态显示内容
  14. //利用计算属性,添加符合时的验证码样式
  15. </div>
  16. <div class='mp2'>
  17. <span>验证码</span>
  18. <input type="text">
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. import Vue from 'vue';
  26. import { Switch } from 'vant';
  27. Vue.use(Switch);
  28. export default{
  29. data()
  30. {
  31. return{
  32. flag:true,
  33. checked:false,
  34. type:'password',
  35. phone:'',
  36. time:0, //倒计时
  37. timeFlag:true //是否能够再次发送验证码
  38. }
  39. },
  40. methods:{
  41. getNum()
  42. {
  43. if(this.right_phone) //调用计算属性,点击时是否能够触发验证码
  44. {
  45. if(this.timeFlag) //倒计时期间的旗帜
  46. {
  47. this.timeFlag=false;
  48. this.time=60;
  49. let timer=setInterval(()=>{
  50. this.time--;
  51. if(this.time==0)
  52. {
  53. this.timeFlag=true;
  54. clearInterval(timer);
  55. }
  56. },1000)
  57. }
  58. }else{
  59. alert('请输入正确手机号');
  60. }
  61. }
  62. },
  63. computed:{ //计算属性动态验证手机号,其中手机号v-model双向绑定
  64. right_phone()
  65. {
  66. return /^1\d{ 10}$/.test(this.phone) //正则匹配是否11个数字,返回布尔值
  67. }
  68. }
  69. }
  70. </script>
  71. <style lang='less'>
  72. .mlog{
  73. margin-top: 20px;
  74. text-align: start;
  75. .mp{
  76. width: 100%;
  77. height: 40px;
  78. line-height: 40px;
  79. white-space: nowrap;
  80. margin-bottom: 20px;
  81. >span:last-child{
  82. color:#ccc;
  83. margin-left: -40px;
  84. }
  85. border: solid 1px #ccc;
  86. >input{
  87. border: none;
  88. }
  89. .rphone{ //手机号格式正确时的验证码样式
  90. color:black !important;
  91. }
  92. }
  93. }
  94. </style>

效果图:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,584人围观)

还没有评论,来说两句吧...

相关阅读

    相关 登录验证

    验证码的作用是区分人与机器,防止机器刷数据; 验证码的验证分了两步,一步是请求获取验证码,一步是前台传的验证码与后台的验证码进行对比判断后进行后续操作; 在MyEclips