此 Validator 物件集中常用表單驗證規則,方便在 jQuery 專案中重複呼叫。以下示例以簡化規則說明,實際專案可依需求調整。
var Validator = {
isEmail: function (s) {
return this.test(s, /^\S+@\S+\.\S+$/);
},
isMobile: function (s) {
return this.test(s, /^1\d{10}$/);
},
isPhone: function (s) {
return this.test(s, /^[0-9]{3,4}-[0-9]{7,8}$/);
},
isPostCode: function (s) {
return this.test(s, /^[1-9][0-9]{5}$/);
},
isEmpty: function (s) {
return !jQuery.isEmptyObject(s);
},
test: function (s, p) {
s = s.nodeType == 1 ? s.value : s;
return new RegExp(p).test(s);
}
};
若需要更嚴格的手機號碼或銀行卡號驗證,建議加入前綴、長度、檢查碼與後端二次驗證,避免只依賴前端格式判斷。