示例工程 25-validation

25-validation/app/index.html

html · 6.4 KB

运行:原生校验 · required / pattern / min / max

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>原生校验 · required / pattern / min / max</title>
    <meta name="description" content="用 HTML 约束做即时校验,并知道何时绕过与如何补服务端校验。" />
    <style>
      * { box-sizing: border-box; }
      body {
        margin: 0;
        min-height: 100vh;
        padding: 32px 20px 40px;
        background: #f8fafc;
        color: #0f172a;
        font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
        line-height: 1.7;
      }
      .page { width: min(100%, 760px); margin: 0 auto; }
      .site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
      .brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
      nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
      nav a + a { margin-left: 14px; }
      main { padding: 28px 0; }
      .eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
      h1 { margin: 6px 0 12px; font-size: clamp(30px, 6vw, 44px); line-height: 1.15; }
      .lead { margin: 0 0 22px; color: #334155; font-size: 17px; }
      section { margin-top: 26px; padding-top: 18px; border-top: 1px solid #e2e8f0; }
      h2 { margin: 0 0 10px; font-size: 22px; }
      p { margin: 0 0 12px; color: #334155; }
      ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
      li + li { margin-top: 4px; }
      form { margin: 0; padding: 16px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
      .field { margin-bottom: 14px; }
      label { display: block; margin-bottom: 4px; font-size: 14px; color: #334155; }
      input { width: 100%; max-width: 420px; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; font-size: 14px; background: #fff; }
      input:user-invalid { border-color: #b91c1c; }
      .help { margin: 4px 0 0; color: #64748b; font-size: 13px; }
      button { margin-top: 4px; padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; font-size: 14px; }
      button[type="reset"] { margin-left: 8px; background: #fff; color: #334155; border-color: #cbd5e1; }
      code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
      pre { margin: 10px 0 0; padding: 14px; overflow-x: auto; border-radius: 10px; background: #0f172a; color: #e2e8f0; font-size: 12px; line-height: 1.7; }
      pre code { padding: 0; background: none; color: inherit; }
      .note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
      footer { margin-top: 30px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
      @media (max-width: 560px) {
        body { padding: 20px 14px 32px; }
        .site-header { flex-direction: column; }
      }
    </style>
  </head>
  <body>
    <div class="page">
      <header class="site-header">
        <span class="brand">FORM NOTES</span>
        <nav aria-label="页内导航">
          <a href="#constraints">约束</a>
          <a href="#api">API</a>
          <a href="#rules">规则</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 25</p>
        <h1>原生校验</h1>
        <p class="lead">
          浏览器能根据 HTML 约束做即时校验:必填、格式、长度、范围。它提供第一道防线,但不能替代服务端。
        </p>

        <section id="constraints">
          <h2>1. 约束属性</h2>
          <form action="preview.html" method="post">
            <div class="field">
              <label for="username">用户名</label>
              <input
                id="username"
                name="username"
                type="text"
                required
                minlength="3"
                maxlength="12"
                pattern="[a-z0-9_]+"
                autocomplete="username"
              />
              <p class="help">3–12 个字符,只含小写字母、数字与下划线。</p>
            </div>

            <div class="field">
              <label for="email">邮箱</label>
              <input id="email" name="email" type="email" required autocomplete="email" />
            </div>

            <div class="field">
              <label for="age">年龄</label>
              <input id="age" name="age" type="number" min="18" max="120" step="1" required />
              <p class="help">18–120 岁。</p>
            </div>

            <div class="field">
              <label for="password">密码</label>
              <input id="password" name="password" type="password" required minlength="8" autocomplete="new-password" />
              <p class="help">至少 8 个字符。</p>
            </div>

            <button type="submit">创建账号</button>
            <button type="reset">清空</button>
          </form>
        </section>

        <section id="api">
          <h2>2. 校验 API</h2>
          <p>需要自定义错误时,脚本可以用约束校验 API:</p>
          <pre><code>input.setCustomValidity("两次密码不一致");
input.reportValidity();          // 显示浏览器提示
form.checkValidity();            // 返回布尔值
input.validity.valueMissing;     // 读取具体原因</code></pre>
          <p class="note">自定义错误要在问题修复后清空:<code>setCustomValidity("")</code>,否则表单永远无效。</p>
        </section>

        <section id="rules">
          <h2>3. 规则</h2>
          <ul>
            <li><code>required</code> 管空值,<code>pattern</code> 管格式,<code>min</code> / <code>max</code> 管范围。</li>
            <li>约束要与服务端规则一致,前端只是体验。</li>
            <li>错误文案用 <code>title</code> 或自定义提示补充,不要只靠浏览器气泡。</li>
            <li>不要给所有字段都加 <code>pattern</code>,复杂规则交给服务端。</li>
            <li><code>novalidate</code> 关闭原生校验时,必须自己实现等价校验。</li>
          </ul>
        </section>
      </main>

      <footer>
        <p>示例数据为虚构,仅用于教学。</p>
      </footer>
    </div>
  </body>
</html>