DIV CSS 佈局教程網

jQuery 驗證工具類整理
編輯:JQuery特效代碼     

驗證工具類

此 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);
  }
};

若需要更嚴格的手機號碼或銀行卡號驗證,建議加入前綴、長度、檢查碼與後端二次驗證,避免只依賴前端格式判斷。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved