示例工程 29-registration-form

29-registration-form/demo-02-errors/index.html

html · 2.2 KB

运行:注册错误演示

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>注册错误演示</title>
    <style>
      body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
      .box { max-width: 720px; margin-bottom: 16px; padding: 18px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
      h1 { margin: 0 0 16px; font-size: 28px; }
      h2 { margin: 0 0 8px; font-size: 17px; }
      p { margin: 0 0 10px; color: #334155; }
      label { display: block; margin: 8px 0 4px; font-size: 14px; }
      input { width: 100%; max-width: 340px; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; }
      input[aria-invalid="true"] { border-color: #b91c1c; }
      .error { margin: 4px 0 0; color: #b91c1c; font-size: 13px; }
      .error::before { content: "错误:"; font-weight: 600; }
      .summary { margin: 0 0 14px; padding: 10px 12px; border: 1px solid #fecaca; border-left: 4px solid #b91c1c; border-radius: 6px; background: #fef2f2; color: #7f1d1d; font-size: 14px; }
      .summary a { color: #7f1d1d; }
    </style>
  </head>
  <body>
    <h1>注册错误</h1>

    <div class="box">
      <h2>错误摘要</h2>
      <div class="summary" role="alert">
        <p>提交时发现 2 个问题:</p>
        <ul>
          <li><a href="#email">邮箱:请输入有效的邮箱地址。</a></li>
          <li><a href="#terms">条款:必须同意后才能注册。</a></li>
        </ul>
      </div>
    </div>

    <div class="box">
      <h2>字段级错误</h2>
      <form action="welcome.html" method="post" novalidate>
        <label for="email">邮箱</label>
        <input id="email" name="email" type="email" aria-invalid="true" aria-describedby="email-error" />
        <p class="error" id="email-error">请输入有效的邮箱地址。</p>

        <label>
          <input type="checkbox" id="terms" name="terms" required aria-invalid="true" aria-describedby="terms-error" />
          同意隐私说明
        </label>
        <p class="error" id="terms-error">必须同意后才能注册。</p>
      </form>
    </div>
  </body>
</html>