在表單提交前(或欄位失焦時),依規則檢查欄位:必填、數值、Email、數值範圍,錯誤則彈出 `alert` 阻止提交。


DW 產生的驗證函式 `MM_validateForm` 內含英文字串,需手動修改代碼:
// 原代碼片段
errors += '- ' + nm + ' is required. ';
// 修改為
errors += '- ' + nm + ' 不能為空。 ';
// 原標題
alert('The following error(s) occurred: ' + errors);
// 修改為
alert('提示:' + errors);
建議將欄位 `name`/`id` 設為中文(如 `姓名`),提示自動帶入中文名稱。
<form>
<input name="email" type="email" required>
<input name="age" type="number" min="18" max="99" required>
<button type="submit">送出</button>
</form>
<script>
document.querySelector('form').addEventListener('submit', e => {
if (!e.target.checkValidity()) {
e.preventDefault();
e.target.reportValidity(); // 瀏覽器原生多語系提示
}
});
</script>
DW 驗證表單行為快速建立基礎檢查;現代專案優先使用 HTML5 內建驗證 + Constraint Validation API,再按需擴充。