js案例——实现验证码功能

布满荆棘的人生 2022-03-26 11:26 475阅读 0赞

今天的内容是用js的基本语法实现网页中常见的验证码功能。

大体思想:核心语法为math中随机数的使用 利用随机数字来实现随机的数字 字符 汉字 ,利用字符串的拼接来实现随机字体大小、旋转角度和各种颜色,利用css样式来禁止从网页上复制和粘贴,最后用弹窗来实现验证结果提示。

实现结果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0lDWV9fXw_size_16_color_FFFFFF_t_70

具体代码及注释:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>验证码</title>
  8. <style>
  9. /* body{
  10. margin: 0%;
  11. padding: 0%;
  12. } */
  13. /* html,body{
  14. -webkit-user-select: none;
  15. -moz-user-select: none;
  16. -ms-user-select: none;} */
  17. /* 分别是谷歌,火狐,IE浏览器的对应禁止选中 */
  18. /* 通过css样式来实现禁止复制验证码 建议使用*/
  19. .block{
  20. width: 100px;
  21. height: 40px;
  22. border: 1px solid grey;
  23. float: left;
  24. background-image: url(img/1.png);
  25. background-size: 100%;
  26. overflow: hidden;
  27. }
  28. #look{
  29. float: left;
  30. color: black;
  31. font-size: 12px;
  32. position: relative;
  33. cursor: pointer;
  34. top: 28px;
  35. left: 5px;
  36. }
  37. #count{
  38. float: left
  39. }
  40. .num{
  41. display: inline-block;
  42. line-height: 40px;
  43. width: 16.5px;
  44. text-align: center;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div>
  50. <div class="block"></div><!--验证码显示区域 需要注意此区域不能复制!-->
  51. <span id="look">看不清...</span><!--防止无法辨识的情况,点击随机生成验证码-->
  52. <div style="clear: both;"><!--用于清除浮动-->
  53. <input type="text" id="count"><!--用户输入栏,将验证码输入在此,且不区分大小写-->
  54. <button id="btn">验证</button><!--点击是比较值,验证成功提示成功,失败提示失败且再次随机生成验证码-->
  55. </div>
  56. </div>
  57. <script>
  58. var block = document.getElementsByClassName('block')[0];
  59. document.onselectstart=function (){
  60. return false;//禁止选择开始
  61. // 建议还是用样式来禁用复制
  62. }
  63. var res;
  64. showdata();
  65. function showdata() {
  66. // 在实现验证码的相应功能时,要有先后顺序,先简单在复杂,要有递进关系
  67. // 个人实现方案:1.先实现数字验证码的随机显示,之后加上点击“看不清”重新随机生成验证码,最后加入验证功能
  68. // 2.在实现了数字验证码的验证后,优化代码,考虑用户输入错误的结果,考虑验证码的显示效果
  69. // 3.最后加入字符和汉字,使得验证码可以随机产生不同类型的样式
  70. res = ''; //比较值的数组
  71. var ele = ''; //拼接值的数组 重新调用时,将值附空
  72. for (var i = 0; i < 6; i++) {//产生6位验证码
  73. if (Math.random() > 0.33) {//产生数字验证码的概率为33%
  74. var number = Math.floor(Math.random() * 10);//Math.random 为0~1且不为1的随机数,*10后向下取整为0~9的随机数
  75. res += number;
  76. ele += "<span class='num' style='transform: rotate(" + Math.floor((Math.random() - 0.5) * 80) +"deg); font-size:" + (Math.floor(Math.random() * 8) + 12) + "px; color:rgb(" + Math.floor(Math.random() *256) + "," + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) +"); '>" + number + "</span>";
  77. // 字符串拼接 需要多练习
  78. } else if (Math.random() > 0.33 && Math.random() < 0.8) {//关于调用的Math.random() 由于是每次调用都会重新产生新的随机数,所以它的值不为固定值。
  79. // 也就是说,上行代码可以换做 Math.random()<0.46 由于它不是固定值,所以它只表示概率!!!
  80. var number = (97 + Math.floor(Math.random() * 26));
  81. var str = ""; //需要转化为字符
  82. if (Math.random > 0.5) {
  83. str = String.fromCharCode(number).toUpperCase(); //小写
  84. } else {
  85. str = String.fromCharCode(number).toUpperCase(); //大写
  86. }
  87. res += str;
  88. ele += "<span class='num' style='transform: rotate(" + Math.floor((Math.random() - 0.5) * 80) +
  89. "deg); font-size:" + (Math.floor(Math.random() * 8) + 12) + "px; color:rgb(" + Math.floor(Math.random() *
  90. 256) + "," + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) +
  91. "); '>" + str + "</span>";
  92. } else {
  93. var str_new = "昨夜雨疏风骤浓睡不消残酒试问卷帘人却道海棠依旧知否知否应是绿肥红瘦";//可以找一段自己喜欢的文字,用来充当文字库
  94. var index=Math.floor(Math.random()*str_new.length);
  95. ele += "<span class='num' style='transform: rotate(" + Math.floor((Math.random() - 0.5) * 80) + "deg); font-size:" + (Math.floor(Math.random() * 8) + 12) + "px; color:rgb(" + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) + "); '>" + str_new.charAt(index) + "</span>";
  96. res+=str_new.charAt(index);
  97. }
  98. }
  99. block.innerHTML = ele;//将拼接的内容赋给block 实现验证码的显示
  100. }
  101. // 看不清
  102. var look = document.getElementById('look');
  103. look.onclick = function () {
  104. showdata();//重新调用即可实现重新生成验证码
  105. }
  106. // 验证按钮
  107. var btn = document.getElementById('btn');
  108. var count = document.getElementById('count');
  109. btn.onclick = function () {
  110. if (count.value.toLowerCase() == res.toLowerCase()) {
  111. alert('验证成功');
  112. // 弹窗
  113. } else {
  114. alert('验证失败');
  115. showdata();
  116. // 重新调用,实现实现重新生成验证码
  117. count.value = '';
  118. //将之前的文本框内容清空
  119. }
  120. }
  121. </script>
  122. </body>
  123. </html>

到此,这个比较简单却功能丰富的验证码案例结束了。

发表评论

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

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

相关阅读

    相关 js实现验证功能

    \前面是拆解着讲的,不想看可以直接跳过,带注释的完整版代码和效果在后面 首先在页面中准备一个输入框,一个显示验证码的盒子和一个提交按钮 <body>