博客列表 >失去焦点进行非空验证

失去焦点进行非空验证

晨
原创
2022年04月07日 15:13:22288浏览

HTML

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>表单事件</title>
  8. <link rel="stylesheet" href="style.css">
  9. </head>
  10. <body>
  11. <form action="" method="post" id="login">
  12. <label class="title">用户登录</label>
  13. <label for="email">邮箱:</label>
  14. <input type="email" id="email" name="email" value="" autofocus />
  15. <label for="password">密码:</label>
  16. <input type="password" id="password" name="password" />
  17. <!-- 1. type="button" 来表示这是一个普通按钮,没有提交行为 -->
  18. <button name="submit" onclick="check(this)">登录</button>
  19. </form>
  20. <script>
  21. document.forms.login.email.onblur = function(){
  22. if(this.value.length === 0){
  23. alert('邮箱不能为空');
  24. return false;
  25. }
  26. };
  27. document.forms.login.password.onblur = function(){
  28. if(this.value.length === 0){
  29. alert('密码不能为空');
  30. return false;
  31. }
  32. };
  33. </script>
  34. </body>
  35. </html>

style.css(表单样式)

  1. body {
  2. background-color: mediumturquoise;
  3. color: #444;
  4. font-weight: lighter;
  5. }
  6. #login {
  7. width: 20em;
  8. border-radius: 0.3em;
  9. box-shadow: 0 0 1em #888;
  10. box-sizing: border-box;
  11. padding: 1em 2em 1em;
  12. margin: 5em auto;
  13. background-color: paleturquoise;
  14. display: grid;
  15. grid-template-columns: 3em 1fr;
  16. gap: 1em 0;
  17. }
  18. #login .title {
  19. grid-area: auto / span 2;
  20. place-self: center;
  21. }
  22. #login input {
  23. border-radius: 0.3em;
  24. border: none;
  25. padding-left: 0.3em;
  26. }
  27. #login input:focus {
  28. outline: none;
  29. box-shadow: 0 0 5px seagreen;
  30. background-color: hsl(283, 100%, 95%);
  31. border-radius: 0.2em;
  32. transition: 0.5s;
  33. }
  34. #login button {
  35. grid-area: auto / 2 / auto;
  36. outline: none;
  37. background-color: lightseagreen;
  38. border: none;
  39. height: 2em;
  40. color: #fff;
  41. }
  42. #login button:hover {
  43. cursor: pointer;
  44. background-color: seagreen;
  45. transition: 0.5s;
  46. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议