html web前端 登录,短信验证码登录

╰半橙微兮° 2024-02-21 11:08 147阅读 0赞

html web前端 登录,短信验证码登录

1,手机号码格式校验
2,按钮点击60秒倒计时,按钮限制点击
3,验证码/或密码长度校验(被注释,公司发的验证码长度不一致,不一定是6位)
4,get网络请求
5,post网络请求 json带参上传
6,服务器返回值打印

format_png

6d4f50def6875c3f0b5b898f83cc4664.jpg

  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>登录</title>
  5. </head>
  6. <body>
  7. <div>
  8. <div style="margin: 10px;">
  9. <input style="width: 200px; " id="phone" type="text" autocomplete="off" placeholder="请输入手机号" />
  10. <input style="width: 100px; " id="btnSendCode" type="button" value="获取验证码" onClick="sendMessage()" />
  11. </div>
  12. <div style="margin: 10px;">
  13. <input style="width: 300px;" id="code" type="text" autocomplete="off" placeholder="请输入验证码" />
  14. </div>
  15. <div style="margin: 15px;">
  16. <button style="width: 100px;" onClick="login()"> 登 录 </button>
  17. <span style="width: 200px;" id="logintext" class="ssss">log打印:</span>
  18. </div>
  19. </div>
  20. </body>
  21. <script type="text/javascript">
  22. var phoneDom = document.querySelector('#phone');
  23. var codeDom = document.querySelector('#code');
  24. var btnSendCode = document.querySelector("#btnSendCode");
  25. var count = 60; //间隔函数,1秒执行
  26. var InterVal; //timer变量,控制时间
  27. //var count;//当前剩余秒数
  28. function SetTime() {
  29. if (curCount == 0) {
  30. window.clearInterval(InterVal); //停止计时器
  31. btnSendCode.removeAttribute("disabled"); //启用按钮
  32. btnSendCode.value = "重新发送";
  33. } else {
  34. curCount--;
  35. btnSendCode.value = curCount + "秒再获取";
  36. }
  37. }
  38. /**
  39. * 获取验证码
  40. */
  41. function sendMessage() {
  42. /
  43. var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/; //手机号正则
  44. var phone = (phoneDom.value).trim();
  45. if (!phoneReg.test(phone)) {
  46. alert(" 请输入有效的手机号码");
  47. return false;
  48. } /
  49. curCount = count;
  50. //设置button效果,开始计时
  51. btnSendCode.setAttribute("disabled", "true");
  52. btnSendCode.value = curCount + "秒再获取";
  53. InterVal = window.setInterval(SetTime, 1000); //启动计时器,1秒执行一次
  54. /
  55. //向后台发送处理数据
  56. // 创建对象
  57. const xhr = new XMLHttpRequest();
  58. xhr.responseType = "json"
  59. //初始化
  60. xhr.open('GET', 'https://api.wzyanche.com/sms/SendSms/' + phoneDom.value);
  61. //发送
  62. xhr.send();
  63. //处理返回值
  64. xhr.onreadystatechange = function() {
  65. if (xhr.readyState === 4) {
  66. if (xhr.status == '200') {
  67. //手动对数据转化
  68. let data = xhr.response;
  69. console.log('111 111 返回的数据', data);
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * 登录
  76. * 提交信息
  77. */
  78. function login() {
  79. // var code = codeDom.value;
  80. // if (!code || code.trim().length != 6) {
  81. // alert(" 请输入6位短信验证码");
  82. // return false;
  83. // }
  84. // 创建一个 XMLHttpRequest 对象
  85. var xhr = new XMLHttpRequest();
  86. // 配置请求
  87. xhr.open('POST', 'https://api.wzyanche.com/cusInfo/login', true);
  88. xhr.setRequestHeader('Content-Type', 'application/json');
  89. // 发送 JSON 数据
  90. var data = {
  91. phone: phoneDom.value,
  92. verificationCode: codeDom.value,
  93. };
  94. xhr.send(JSON.stringify(data));
  95. // 监听请求的状态
  96. xhr.onreadystatechange = function() {
  97. if (xhr.readyState === 4 && xhr.status === 200) {
  98. // 请求成功后的处理
  99. console.log(xhr.responseText);
  100. // 打印,获取json里的对象
  101. var data2 = JSON.parse(xhr.responseText)
  102. console.log('222 222 返回的数据', data2.retMsg);
  103. // 打印返回值
  104. document.getElementById("logintext").textContent = data2.retMsg;
  105. }
  106. };
  107. }
  108. </script>
  109. </html>

发表评论

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

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

相关阅读