不想背 framework 的 API,又想快速得到一套能用的驗證機制?最直白的做法是:在每個 input 上寫一個 validate 屬性,內容是分號分隔的驗證函式名稱,然後用 eval 把它們串起來執行。以下拆解這套設計的關鍵。
整個框架的樞紐只有一行:
if (!eval(scriptCode)) {
return false;
}
呼叫方式則完全宣告在標籤上:
<input type="text" class="text_field percentCheck" name="progress_payment_two"
id="progress_payment_two" inputName="進度款2比例(%)"
validate="isNumeric;notNull" maxLength="350" value="" />
驗證時會依序自動呼叫 isNumeric 與 notNull,任一失敗就回傳錯誤訊息。的好處是新增規則只要寫一個函式,不必改動框架本身。
進入點會遍歷所有表單與元素,跳過 hidden、button、被 CSS 隱藏的欄位,以及 id 帶 TF_ 標記的特殊控制項。
function checkAllForms() {
var checkResult = true;
rmTempStatusIsAlert = false;
rmTempStatusIsFocus = false;
setAllVenusInputsDefault();
for (var i = 0; i < document.forms.length; i++) {
for (var j = 0; j < document.forms[i].elements.length; j++) {
var thisInput = document.forms[i].elements[j];
if (thisInput.type != "hidden" && thisInput.type != "button"
&& thisInput.clientWidth != 0
&& thisInput.style && thisInput.style.display != 'none') {
var rtValue = check(thisInput);
if (checkResult && rtValue == false) {
checkResult = false;
break;
}
}
}
}
return checkResult;
}
function check(thisInput) {
var validateStr = thisInput.validate;
if (validateStr == null) return true;
var inputValue = thisInput.value;
var validateTemp = validateStr.split(';');
for (var i = 0; i < validateTemp.length; i++) {
if (validateTemp[i].length == 0) continue;
var s = replaceSingleQuote(inputValue);
try {
var scriptCode = "javascript:" + validateTemp[i];
if (validateTemp[i].indexOf("(") < 0) {
scriptCode = "javascript:" + validateTemp[i] + "(s,thisInput)";
}
if (!eval(scriptCode)) return false;
} catch (e) {
alert("校驗函數" + validateTemp[i] + "有異常,請檢查!\n" + e.message);
return false;
}
}
return true;
}
這裡有兩個值得學的細節:一是 replaceSingleQuote 會把單引號、括號、分號等字元轉成 Unicode 跳脫序列,避免使用者輸入破壞字串結構;二是包了 try/catch,驗證函式寫錯時會明確提示,而不是靜默失敗。
writeValidateInfo 依全域設定 ConfigValidateInfoType 決定要用 alert 彈窗、在欄位後插入提示文字,或兩者並用;同時會呼叫 setVenusInputError 把焦點移到出错欄位並加上虛線邊框與高亮底色。這裡刻意避開 _frm.click(),因為在 file upload 欄位上觸發 click 會出錯;也對 SELECT 排除 select() 呼叫,因為下拉選單不支援選取文字。
框架附帶的規則相當完整,常用的這幾個可以直接拿來改:
function notNull(s, thisInput) { // 去空白後不可為空
try { thisInput.value = thisInput.value.trim(); } catch (e) {}
s = s.trim();
if (s.length == 0) { writeValidateInfo("不能為空或空格!", thisInput); return false; }
return true;
}
function isMobile(s, thisInput) { // 手機號碼
s = s.trim();
if (s.length == 0) return true;
if (s.length != 11) { writeValidateInfo('請輸入合法的手機號碼!', thisInput); return false; }
var prefixes = ["13", "14", "15", "18"];
for (var i = 0; i < prefixes.length; i++) {
if (s.substring(0, 2) == prefixes[i]) return true;
}
writeValidateInfo('請輸入合法的手機號碼!', thisInput);
return false;
}
function isEmail(s, thisInput) { // Email
s = s.trim();
if (s.length == 0) return true;
if (s.length > 100) { writeValidateInfo("email地址長度不能超過100位!", thisInput); return false; }
var regu = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
if (!regu.exec(s)) { writeValidateInfo("請輸入有效合法的E-mail地址!", thisInput); return false; }
return true;
}
除了這些,原始碼還涵蓋身分證、郵遞區號、傳真與座機號碼、中文與非中文字元、整數與浮點、IP、以及大量「整數位/小數位」精度組合(如 isNum18p2、isNum20p6),並附了密碼強度評分 pwdStrength 與年度區間比較 validateYear。建議直接下載原檔瀏覽,比逐條抄寫有效率得多。
這套寫法誕生於 eval 還沒被列為禁忌的年代。今天若重做同樣的事,會建議改用 new Function 或直接把規則表成 { rule: params } 的資料結構再查表分派,並搭配 novalidate 與 Constraint Validation API,兼顧安全性與可維護性。