验证插件:/*
 * 企学家公共类 表单验证插件
 *
 * 插件盒子:form
 * 注册名称:verify、表单验证
 *
 * 22:37 2011-2-27 By Casey
 */  (function() {
    //创建插件对象
    var _bin = function(that, p/* form, request */) {
      try {
        var css = [/* 样式选择器 */,
          /* 输入样式 */ p.cssinput || "verifyinput",
          /* 空缺样式 */ p.cssempty || "verifyempty",
          /* 错误样式 */ p.csserror || "verifyerror",
          /* 正确样式 */ p.cssright || "verifyright"
        ];
        var tip = [/* 提示选择器 */,
          /* 输入提示 */ /input=(\S+);/,
          /* 空缺提示 */ /empty=(\S+);/,
          /* 错误提示 */ /error=(\S+);/,
          /* 正确提示 */ /right=(\S+);/
        ];
        var obj = [/* 对象选择器 {
          e : element,
          r : request,
          t : tipbox,
          s : status
        }*/];
        var rqt = {/* 请求选择器 */
          /* 方法 */
          "verify" : function(i) {
            var e = obj[i].e;
            if(!e.value) {
              e.getAttribute("empty") ? obj[i].s = 0 : obj[i].s = 2;
              return rqt["depict"](i);
            }
            var a = e.getAttribute("regex");
            if(a && e.value.search(a) == -1) {
              obj[i].s = 3;
              return rqt["depict"](i);
            }
            var a = e.getAttribute("equal");
            if(a && e.value != obj[a].e.value) {
              obj[i].s = 3;
              return rqt["depict"](i);
            }
            var a = e.getAttribute("quest");
            if(a) {
              v = p.request[a](e);
              if(v) {
                obj[i].s = 3;
                return rqt["depict"](i, v);
              }
            }
            obj[i].s = 4;
            return rqt["depict"](i);
          },
          "behove" : function(i) {
            for(var v = 0, j = 0; j < obj[i].e.length; j++) {
              v = v || obj[i].e[j].checked;
            }
            v ? obj[i].s = 4 : obj[i].s = 2;
            return rqt["depict"](i);
          },
          "depict" : function(i, v) {
            var e = obj[i].e[0] || obj[i].e;
            var t = v || ((e.getAttribute("tip") || "").match(tip[obj[i].s]) || ["", ""])[1] || "";
            if(obj[i].t === undefined) {
              obj[i].t = null;
              that.ergodicElement(e.parentNode, function(e) {
                if(e.getAttribute("tipbox")) {
                  obj[i].t = e;
                }
              });
            }
            if(obj[i].t) {
              obj[i].t.innerHTML = obj[i].s ? "<span class=\"" + css[obj[i].s] + "\">" + t + "<\/span>" : "";
              return false;
            } else {
              return /0|4/.test(obj[i].s) ? false : t;
            }
          },
          "submit" : function() {
            for(var t = [], i = 0, j = 0; j < obj.length; j++) {
              var d = obj[j].r(j);
              d ? t.push(d) : null;
              i += /0|4/.test(obj[j].s) ? 0 : 1;
            }
            if(p.request["submit"]) {
              var d = p.request["submit"](f, i);
              d ? t.push(d) : null;
              i += d ? 1 : 0;
            }
            if(i) {
              try {
                var clear = that.plugins("dialog", "alert")({
                    head : "提交",
                    text : "您提交的表单中有 " + i + " 处错误,请更正后再提交!" + (t.length ? "\n\n您可能需要确认以下内容:\n※ " + t.join("\n※ ") : ""),
                  button : {
                    "确定" : function() {clear()}
                  }
                });
              } catch(error) {
                alert("您提交的表单中有 " + i + " 处错误,请更正后再提交!" + (t.length ? "\n\n您可能需要确认以下内容:\n\n※ " + t.join("\n※ ") : ""));
              }
            } else {
              for(var j = 0; j < obj.length; j++) {
                try {
                  obj[j].e.setAttribute("readonly", "readonly", 0);
                } catch(error) {
                  continue;
                }
              }
            }
            return i;
          },
          /* 动作 */
          "onsubmit" : function() {
            if(!rqt["submit"]()) {
              this.onclick = function() {};
              f.submit();
            }
          },
           "onreset" : function() {
            try {
              var clear = that.plugins("dialog", "alert")({
                  head : "重置",
                  text : "您可能丢失尚未保存的信息,确认重置?",
                button : {
                  "确定" : function() {f.reset(); clear()},
                  "取消" : function() {clear()}
                }
              });
            } catch(error) {
              if(confirm("您可能丢失尚未保存的信息,确认重置?")) {
                f.reset();
              }
            }
          },
          "onsaveas" : function() {
            if(!rqt["submit"]()) {
              try {
                var e = this;
                var clear = that.plugins("dialog", "alert")({
                    head : "保存",
                    text : "保存操作将更新当前的记录,确认保存?",
                  button : {
                    "确定" : function() {e.onclick = function() {}; f.submit()},
                    "取消" : function() {clear()}
                  }
                });
              } catch(error) {
                if(confirm("保存操作将更新当前的记录,确认保存?")) {
                  this.onclick = function() {};
                  f.submit();
                }
              }
            }
          },
          "oncancel" : function() {
            try {
              var e = this;
              var clear = that.plugins("dialog", "alert")({
                  head : "取消",
                  text : "您可能丢失尚未保存的信息,确认取消?",
                button : {
                  "确定" : function() {that.action("goto", e.getAttribute("goto"))},
                  "取消" : function() {clear()}
                }
              });
            } catch(error) {
              if(confirm("您可能丢失尚未保存的信息,确认取消?")) {
                that.action("goto", this.getAttribute("goto"));
              }
            }
          }
        };
        var f = that.$(p.form);
        f.setAttribute("onsubmit", "return false");
        that.ergodicElement(f, function(e) {
          var a = e.getAttribute("quest") ? "quest" :
                  e.getAttribute("regex") ? "regex" :
                  e.getAttribute("equal") ? "equal" :
                  e.getAttribute("fixed") ? "fixed" : null;
          if(/quest|regex|equal/i.test(a) && (/text|password/i.test(e.type) || /textarea/i.test(e.tagName))) {
            e.onfocus = function() {
              var i = this.getAttribute("verifyindex");
              if(/0|1|2/.test(obj[i].s)) {
                obj[i].s = 1;
                rqt["depict"](i);
              }
            };
            e.onblur = function() {
              rqt["verify"](this.getAttribute("verifyindex"));
            };
            e.setAttribute("verifyindex", obj.length);
            obj.push({
              e : e,
              r : rqt["verify"],
              s : 1
            });
          } else if(/fixed/i.test(a) && /checkbox|radio/i.test(e.type)) {
            e = e.name ? document.getElementsByName(e.name) : [e];
            for(var i = 0; i < e.length; i++) {
              e[i].onclick = function() {
                rqt["behove"](this.getAttribute("verifyindex"));
              };
              e[i].setAttribute("verifyindex", obj.length);
            }
            obj.push({
              e : e,
              r : rqt["behove"]
            });
          } else if(/quest/i.test(a)) {
            e.onclick = rqt[e.getAttribute("quest")];
          }
        });
      } catch(error) {      }
    };    //注册插件
    qixuejia.bin.form["verify"] = _bin;
    qixuejia.bin.form["表单验证"] = _bin;
  })();

解决方案 »

  1.   

    框架主体:/*
     * 企学家公共类
     *
     * 18:58 2011-2-27 By Casey
     */  //定义命名空间
      var qixuejia = qixuejia || {};  //创建配置对象
      qixuejia.cfg = {};  //创建插件对象
      qixuejia.bin = {
        action : {
        /* 动作插件盒子 */
        },
        control : {
        /* 控件插件盒子 */
        },
        plugins : {
        /* 组件插件盒子 */
        },
        form : {
        /* 表单插件盒子 */
        }
      };  //创建类对象
      qixuejia.min = function() {
        //工具函数
        var _$ = function(n, w) {
          return typeof n == "string" ? (w || window).document.getElementById(n) : n;
        };    var _getElementByStyle = function(n, w) {
          return _$(n, w).currentStyle || (w || window).getComputedStyle(_$(n, w), null);
        };    var _getElementBySite = function(n, w) {
          for(var t = 0, l = 0, e = _$(n, w); e; e = e.offsetParent) {
            t += e.offsetTop;
            l += e.offsetLeft;
          }
          return {
             top : t,
            left : l
          };
        };    var _getDocumentByScroll = function(w) {
          var w = w || window;
          var x = w.document.documentElement.scrollLeft || w.document.body.scrollLeft || 0;
          var y = w.document.documentElement.scrollTop  || w.document.body.scrollTop  || 0;
          return {
            x : x,
            y : y
          };
        };    var _getDocumentBySize = function(w) {
          var w = w || window;
          if(w.innerWidth) {
            return {
               width : w.innerWidth,
              height : w.innerHeight
            };
          } else if(w.document.documentElement.clientWidth) {
            return {
               width : w.document.documentElement.clientWidth,
              height : w.document.documentElement.clientHeight
            };
          } else if(w.document.body.clientWidth) {
            return {
               width : w.document.body.clientWidth,
              height : w.document.body.clientHeight
            };
          }
        };    var _getBrowserByType = function() {
          var b, v, t;
          var ua = navigator.userAgent;
          (b = ua.match(/msie ([\d.]+)/i            )) ? (v = b[1], t = 1, b = "Internet Explorer") :
          (b = ua.match(/firefox\/([\d.]+)/i        )) ? (v = b[1], t = 2, b = "Firefox"          ) :
          (b = ua.match(/version\/([\d.]+).*safari/i)) ? (v = b[1], t = 3, b = "Safari"           ) :
          (b = ua.match(/opera.([\d.]+)/i           )) ? (v = b[1], t = 4, b = "Opera"            ) :
          (b = ua.match(/chrome\/([\d.]+)/i         )) ? (v = b[1], t = 5, b = "Chrome"           ) : null;
          return {
            browser : b,
            version : v,
               type : t
          };
        };    var _ergodicElement = function(n, f, w) {
          var e = _$(n, w);
          if(e.nodeType == 1) {
            f(e);
          }
          if(e.firstChild) {
            for(var n = e.firstChild; n; n = n.nextSibling) {
              _ergodicElement(n, f, w);
            }
          }
        };    var _bubbleElement = function(e, f) {
          for(var e = e.target || e.srcElement; e; e = e.parentNode) {
            if(f(e)) {
              return e;
            }
          }
        };    //事件函数
        var _attachEvent = function(n, e) {
          if(document.addEventListener) {
            for(var a in e) {
              n.addEventListener(a, e[a], false);
            }
          } else if(document.attachEvent) {
            for(var a in e) {
              n.attachEvent("on" + a, e[a]);
            }
          }
        };    var _detachEvent = function(n, e) {
          if(document.removeEventListener) {
            for(var a in e) {
              n.removeEventListener(a, e[a], false);
            }
          } else if(document.detachEvent) {
            for(var a in e) {
              n.detachEvent("on" + a, e[e]);
            }
          }
        };    var _cancelBubble = function(e) {
          if(e.stopPropagation) {
            e.stopPropagation();
          } else {
            e.cancelBubble = true;
          }
        };    var _cancelDefault = function(e) {
          if(e.preventDefault) {
            e.preventDefault();
          } else {
            e.returnValue = false;
          }
        };    //工厂函数
        var _createElement = function(n, a, c, w) {
          var w = w || window;
          var e = w.document.createElement(n);
          if(a && a.constructor != Object) {
            c = a;
            a = null;
          }
          if(a) {
            for(var n in a) {
              if(n == "style"      ) {e.style.cssText = a[n]; continue}
              if(n == "className"  ) {e.className     = a[n]; continue}
              if(n == "onclick"    ) {e.onclick       = a[n]; continue}
              if(n == "onmouseover") {e.onmouseover   = a[n]; continue}
              if(n == "onmouseout" ) {e.onmouseout    = a[n]; continue}
              if(n == "onmousedown") {e.onmousedown   = a[n]; continue}
              if(n == "onmouseup"  ) {e.onmouseup     = a[n]; continue}
              e.setAttribute(n, a[n]);
            }
          }
          if(c) {
            if(c.constructor == Array) {
              for(var i = 0; i < c.length; i++) {
                if(c[i].constructor == String) {
                  c[i] = w.document.createTextNode(c[i]);
                }
                e.appendChild(c[i]);
              }
            } else if(c.constructor == String) {
              e.appendChild(w.document.createTextNode(c));
            } else {
              e.appendChild(c);
            }
          }
          return e;
        };    var _createAnimate = function() {    };    //扩展函数
        var _action = function(n, p) {
          return qixuejia.bin.action[n].call(this, this, p);
        };    var _control = function(n, p) {
          return qixuejia.bin.control[n].call(this, this, p);
        };    var _plugins = function(n, p) {
          return qixuejia.bin.plugins[n].call(this, this, p);
        };    var _form = function(n, p) {
          return qixuejia.bin.form[n].call(this, this, p);
        };    //错误处理
        var _error = function(m, u, l) {
          alert(u +
                "\n__________________________________________________" +
                "\n" +
                "\n Line:" + l +
                "\nError:" + m);
          return true;
        };    //添加引用
        this.$                   = _$                   ;
        this.getElementByStyle   = _getElementByStyle   ;
        this.getElementBySite    = _getElementBySite    ;
        this.getDocumentByScroll = _getDocumentByScroll ;
        this.getDocumentBySize   = _getDocumentBySize   ;
        this.getBrowserByType    = _getBrowserByType    ;
        this.ergodicElement      = _ergodicElement      ;
        this.bubbleElement       = _bubbleElement       ;
        this.attachEvent         = _attachEvent         ;
        this.detachEvent         = _detachEvent         ;
        this.cancelBubble        = _cancelBubble        ;
        this.cancelDefault       = _cancelDefault       ;
        this.createElement       = _createElement       ;
        this.createAnimate       = _createAnimate       ;
        this.action              = _action              ;
        this.control             = _control             ;
        this.plugins             = _plugins             ;
        this.form                = _form                ;
        this.error               = _error               ;
      };
      

  2.   

    框架默认插件:
      //创建默认插件
      (function() {    /******************** ACTION BIN ********************/
        var _bin = function(that, p) {
          try {
            window.location.href = encodeURI(encodeURI(p));
          } catch(error) {      }
        };
        qixuejia.bin.action["goto"] = _bin;
        qixuejia.bin.action["跳转"] = _bin;
        /******************** ACTION BIN ********************/
        /******************** ACTION BIN ********************/
        var _bin = function(that, p) {
          try {
            window.open(encodeURI(encodeURI(p)));
          } catch(error) {      }
        };
        qixuejia.bin.action["open"] = _bin;
        qixuejia.bin.action["打开"] = _bin;
        /******************** ACTION BIN ********************/
        /******************** ACTION BIN ********************/
        var _bin = function(that, p/* target */) {
          try {
            var e = that.$(p.target);
            if(that.getElementByStyle(e).display != "none") {
              e.style.display = "none";
            } else {
              e.style.display = "block";
            }
          } catch(error) {      }
        };
        qixuejia.bin.action["show"] = _bin;
        qixuejia.bin.action["显示"] = _bin;
        /******************** ACTION BIN ********************/
        /******************** ACTION BIN ********************/
        var _bin = function(that, p/* window, event, target */) {
          try {      } catch(error) {      }
        };
        qixuejia.bin.action["drag"] = _bin;
        qixuejia.bin.action["拖动"] = _bin;
        /******************** ACTION BIN ********************/
        /******************** ACTION BIN ********************/
        var _bin = function(that, p) {
          try {
            document.cookie = "test=test";
            return document.cookie.indexOf("test=test") > -1 ? 1 : 0;
          } catch(error) {      }
        };
        qixuejia.bin.action["checkcookie"] = _bin;
        qixuejia.bin.action["检查饼干"] = _bin;
        /******************** ACTION BIN ********************/
        /********************** FORM BIN ********************/
        var _bin = function(that, p/* root, button */) {
          try {
            var c = [];
            var b = [];
            that.ergodicElement(that.$(p.root), function(e) {
              if(/checkbox/i.test(e.type)) {
                c.push(e);
                that.attachEvent(e, {
                  click : function(event) {
                    that.cancelBubble(event || window.event);
                  }
                });
              }
              if(e.tagName == p.button.toUpperCase()) {
                b.push(e);
                e.setAttribute("onselectindex", b.length);
                that.attachEvent(e, {
                  click : function(event) {
                    var t = that.bubbleElement(event || window.event, function(e) {return e.getAttribute("onselectindex") ? 1 : 0});
                    var n = c[t.getAttribute("onselectindex") - 1];
                    n.checked = !n.checked;
                  }
                });
              }
            });
          } catch(error) {      }
        };
        qixuejia.bin.form["onselect"] = _bin;
        qixuejia.bin.form["选择"] = _bin;
        /********************** FORM BIN ********************/
        /********************** FORM BIN ********************/
        var _bin = function(that, p/* root, button */) {
          try {
            var r = that.$(p.root).getElementsByTagName("input");
            var b = that.$(p.button);
            var i = b.innerHTML;
            var c = true;
            that.attachEvent(b, {
              click : function(event) {
                for(var j = 0; j < r.length; j++) {
                  if(/checkbox/.test(r[j].type)) {
                    r[j].checked = c;
                  }
                }
                if(i) {
                  if(c) {
                    b.innerHTML = "取消" + i;
                  } else {
                    b.innerHTML = i;
                  }
                }
                c = !c;
              }
            });
          } catch(error) {      }
        };
        qixuejia.bin.form["onselectall"] = _bin;
        qixuejia.bin.form["全选"] = _bin;
        /********************** FORM BIN ********************/  })();
      

  3.   

    调用:
      window.onload = function() {
        p.form("verify", {
             form : "form",
          request : {
            /* 提交函数,可以没有 */
            submit : function(f/* 表单对象 */, i/* 错误数量 */) {
              return /* 返回false,或错误信息 */
            }
            /* 自定义验证函数 */
            vmail : function(e/* 表单input对象 */) {
              alert(e.value);
              retrun /* 返回false,或错误信息 */
            } 
          }
        });
      };<form id="form" action="URL">
      <div>
        邮箱(方法验证):
        <input type="text"
               regex="^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
               quest="vmail"
               tip="input=输入提示; empty=空缺提示; error=错误提示; right=恭喜您注册邮箱可以使用。;" />
        <span tipbox="tipbox"></span>
      </div>
      <div>
        邮箱(可为空):
        <input type="text"
               regex="^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
               empty="empty"
               tip="input=输入提示; empty=空缺提示; error=错误提示; right=恭喜您注册邮箱可以使用。;" />
        <span tipbox="tipbox"></span>
      </div>
      <div>
        邮箱(无提示盒子):
        <input type="text"
               regex="^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
               tip="input=输入提示; empty=空缺提示; error=错误提示; right=恭喜您注册邮箱可以使用。;" />
      </div>
      <div>
        checkbox
        <input type="checkbox" fixed="fixed" tip="empty=请阅读并接受“服务条款”。;" />
        我已经阅读并接受“<a href="javascript:void 0;" onclick="agree()">服务条款</a>”。
      </div>
      <input type="button" value="提交" quest="onsubmit" />
      <input type="button" value="重置" quest="onreset" />
      <input type="button" value="保存" quest="onsaveas" />
      <input type="button" value="取消" quest="oncancel" />
    </form>
      

  4.   

    说明:  想使用提示盒子效果,必须保证设置有tipbox属性的节点在input的父节点中,如:
      <div>
        邮箱:
        <input type="text"
               regex="..."
               tip=”..." />
        <span tipbox="tipbox"></span>
      </div>  如果一个需要验证的内容没有设置提示盒子,那么提示信息将汇总到最后提交时候的对话框中显示。  提示盒子默认使用这4个样式名:
      输入样式:verifyinput
      空缺样式:verifyempty
      错误样式:verifyerror
      正确样式:verifyright  如有需要也可以在配置插件时候自定义样式名,如:
      window.onload = function() {
        p.form("verify", {
          ...
          cssempty : "verifyerror"
          ...
        });
      };  对于想进行验证的内容可以设置下面的html属性,设置后插件将自动扫描,并列入验证序列。
      正则验证:repex="正则表达式"
      比较验证:equal="序列位置号" 跟验证序列中第几个内容比较全等
      方法验证:quset="方法名称"
      必须选定:fixed="fixde" 这个属性对于单选或多选按钮起作用,如果是一组带有name属性的按钮,请将该属性设置到第一个按钮上。  辅助属性:
      内容可为空:empty="empty"