当前位置:Gxlcms > JavaScript > JS验证不重复验证码

JS验证不重复验证码

时间:2021-07-01 10:21:17 帮助过:3人阅读

话不多说,请看代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>不重复验证码</title>
  6. <style>
  7. .cont{width:300px;height:auto;margin:50px auto;}
  8. #useId{width:120px;height:45px;line-height:45px;background:#ccc;text-align:center;font-size:20px;margin:10px;}
  9. </style>
  10. </head>
  11. <body>
  12. <div class="cont">
  13. <div id="useId"></div>
  14. <input type="text" id="txt" />
  15. <input type="button" id="btn" value="Start" />
  16. <div>
  17. <script>
  18. function $(id){
  19. return document.getElementById(id);
  20. }
  21. var $useId = $('useId'),
  22. $txt = $('txt'),
  23. $btn = $('btn');
  24. function getStr(){
  25. var string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
  26. str = '';
  27. while(str.length < 6){
  28. var str1 = string.charAt(Math.round(Math.random() * 56));
  29. if(str.indexOf(str1) === -1){
  30. str += str1;
  31. }
  32. }
  33. $useId.innerHTML = str;
  34. }
  35. function checkCode(){
  36. if($txt.value == $useId.innerHTML){
  37. alert('验证成功');
  38. }else{
  39. alert('验证失败');
  40. }
  41. }
  42. $useId.onclick = getStr;
  43. $btn.onclick = checkCode;
  44. </script>
  45. </body>
  46. </html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

人气教程排行