基於jquery的驗證類庫(4) .


var validateFields = {
    validType: [vEmpty, vLength, vCheck, vFloat, vRange, vEmail],
    preNotValidCtl: undefined,
    preNotValidType: undefined,
    validateAll: function() {

        this.preNotValidCtl = undefined;
        this.preNotValidType = undefined;
        var base = this;

        $(".validErr").removeClass("validErr");

        for (var idx = 0, count = this.validType.length; idx < count; idx++) {
            var validator = this.validType[idx];

            $("." + validator.name).each(function() {
                var $ctl = $(this);
                if ($ctl.parent().is(":visible")) {
                    if (!validator.vd($ctl)) {

                        ($ctl.attr("type") == "hidden") ?
          $ctl.parent().addClass("validErr") : $ctl.addClass("validErr");

                        base.preNotValidType = validator;
                        base.preNotValidCtl = $ctl;
                        base.showWarning(validator, $ctl);

                        return false;
                    }
                }
            });

            if (this.preNotValidCtl) {
                break;
            }
        }

        return this.preNotValidCtl == undefined;
    },

    showWarning: function(validator, $ctl) {

        // 元件本身有設定錯誤訊息
        var warning = "問題:" + validateFields.preNotValidType.warning + "\n";
        var conditions = [];

        for (var idx = 0, count = validateFields.validType.length; idx < count; idx++) {
            var ov = validateFields.validType[idx];
            if (ov.name != validator.name) {
                if ($ctl.hasClass(ov.name)) {
                    ov.vd($ctl);
                    conditions.push(ov.getWarning($ctl));
                }
            }
        }

        if (conditions.length > 0) {
            warning += "\n其它限制:" + conditions.join("、");
        }

        alert(warning);

        if ($ctl.attr("type") == "hidden") {
            if ($ctl.parent()) {
                $ctl.parent().focus();
            }
        }
        else if (!$ctl.is(":hidden")) {
            $ctl.focus();
        }
    }
};
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值