8

这个错误让我发疯,一切似乎都井井有条,但希望我错过了一些简单的东西。

我的 aspx 中的代码

        $('#myForm').validate({
            rules: {
                'ctl00$SecondaryPlaceHolder$txPayloadDate': {
                    required: true,
                    date: true
                },
                'ctl00$SecondaryPlaceHolder$ddlMapPlat': {
                    required: true
                },
                'ctl00$SecondaryPlaceHolder$txtBlock': {
                    maxLength: 3
                },
                'ctl00$SecondaryPlaceHolder$txtLeakNumber': {
                    number: true,
                    maxLength: 27
                },
                'ctl00$SecondaryPlaceHolder$txtHouseNumber': {
                    required: true,
                    maxLength: 10,
                },
                'ctl00$SecondaryPlaceHolder$txtStreet': {
                    required: true,
                    maxLength: 100
                },
                'ctl00$SecondaryPlaceHolder$txtCity': {
                    required: true,
                    maxLength: 50
                },
                'ctl00$SecondaryPlaceHolder$txtReading': {
                    isPositiveInteger: true,
                    maxLength: 100
                },
                'ctl00$SecondaryPlaceHolder$ddlInfoCodes': {
                    existsWithLowReading: true
                },
                'ctl00$SecondaryPlaceHolder$txtLocationRemarks': {
                    maxLength: 500
                },
                'txtGrade2RequestedRepairDate': {
                    date: true
                },
                'ctl00$SecondaryPlaceHolder$ddlEquipment': {
                    required: true
                }
            }
        });

自定义验证

$.validator.addMethod("isPositiveInteger",
    function (value, element) {
        if($.trim(value) !== '')
            return /^\d+$/.test(value);
        return true;
    },
    "Must be a valid integer."
);


$.validator.addMethod("existsWithLowReading",
    function (value, element) {
        if (parseFloat($('#SecondaryPlaceHolder_txtReading').val() <= 2) && value.length < 1) {
            return false;
        }
        return true;
    },
    "Info Code required if Reading % is less than three."
);

基本上问题不在自定义验证范围内,但我还是把它们扔在这里......提交时表单验证正常,但是有一些“门牌号”和“街道”在尝试填写它们时会抛出此错误在。

例如,我提交带有空门牌号的表单,验证有效,但是当我填写输入时,在模糊时抛出此错误。

错误在 jquery.validate 中就var rule = { method: method, parameters: rules[method] }行了:

 check: function (element) {
            element = this.validationTargetFor(this.clean(element));

            var rules = $(element).rules();
            var dependencyMismatch = false;
            var val = this.elementValue(element);
            var result;

            for (var method in rules) {
                var rule = { method: method, parameters: rules[method] };
                try {

                    result = $.validator.methods[method].call(this, val, element, rule.parameters);

                    // if a method indicates that the field is optional and therefore valid,
                    // don't mark it as valid when there are no other rules
                    if (result === "dependency-mismatch") {
                        dependencyMismatch = true;
                        continue;
                    }
                    dependencyMismatch = false;

                    if (result === "pending") {
                        this.toHide = this.toHide.not(this.errorsFor(element));
                        return;
                    }

                    if (!result) {
                        this.formatAndAdd(element, rule);
                        return false;
                    }
                } catch (e) {
                    if (this.settings.debug && window.console) {
                        console.log("exception occured when checking element " + element.id + ", check the '" + rule.method + "' method", e);
                    }
                    throw e;
                }
            }

在此先感谢您的任何指点

4

1 回答 1

13

我的愚蠢错字是罪魁祸首

maxLength 不是驼峰式的,应该是 maxlength

虽然 equalTo 是驼峰式的......似乎不一致,但很高兴我明白了

于 2012-10-17T22:10:04.500 回答