示例工程 25-validation
25-validation/demo-01-constraints/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;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: 360px; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; }
input:user-invalid { border-color: #b91c1c; }
.help { margin: 2px 0 8px; color: #64748b; font-size: 13px; }
button { margin-top: 8px; padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
</style>
</head>
<body>
<h1>约束演示</h1>
<div class="box">
<h2>四类约束</h2>
<form action="preview.html" method="post">
<label for="code">邀请码(6 位大写字母)</label>
<input id="code" name="code" type="text" required pattern="[A-Z]{6}" title="请输入 6 位大写字母" />
<p class="help">required + pattern + title</p>
<label for="count">数量(1–5)</label>
<input id="count" name="count" type="number" min="1" max="5" step="1" required />
<p class="help">min + max + step</p>
<label for="nick">昵称(2–10 个字符)</label>
<input id="nick" name="nick" type="text" required minlength="2" maxlength="10" />
<p class="help">minlength + maxlength</p>
<button type="submit">提交</button>
</form>
</div>
</body>
</html>