data-rules 屬性中。length、尾隨逗號)。<form id="regForm">
<input type="text" name="email" value="@"
data-rules="郵箱-required#郵箱-valid_email" />
<input type="password" name="password" id="txt_pass"
data-rules="密碼-min_length+8#密碼-max_length+16" />
<input type="password" name="password2"
data-rules="確認密碼-matches+txt_pass+密碼" />
<button type="submit">註冊</button>
</form>
| 格式 | 含義 |
|---|---|
標題-規則名 | 單規則,標題用於錯誤提示 |
標題-規則名+參數 | 帶參數規則(如 min_length+8) |
規則1#規則2#規則3 | 多規則用 # 分隔,按序執行 |
matches+元素ID+標題 | 比對另一字段值(確認密碼) |
required:非空valid_email:郵箱格式min_length+n / max_length+n:長度範圍matches+id+title:與指定 ID 元素值相等$(function() {
$('form').on('submit', function() {
var msg = $(this).validate(); // 返回 true 或錯誤信息字符串
if (msg !== true) {
alert(msg); // 或自定義提示 UI
return false; // 阻止提交
}
return true; // 允許提交
});
});
原版 jquery.simple.validation.js 在 IE 下報錯,已修復:
length 重命名避免保留字衝突。