jQuery 验证 this.optional 不是函数

jQuery Validation this.optional is not a function

我创建了自己的自定义验证方法,该方法依次使用 minlength 方法,但出现此错误:

this.optional is not a function

我的代码在这里:

jQuery.validator.addMethod("validAccount", function(value, element, params) {
        var res = true;
        if (!isNaN(parseInt(value[0]))) {
            res = false;
        } else if (!$.validator.methods.minlength(value.split(""), element, params)) {
            res = false;
        }
        return res;
    }, "The inserted account isn't valid");

谁能帮帮我?我想了解问题出在哪里。

注意:在 minlength 方法中我使用了 split,因为没有它我也得到 getLength is not a function。

引用 OP :

"The account field shouldn't start for a number, there is no such built-in method. So I should create it. Also, the account field should be at least n characters. I thought about using the existing method for this instead of re-created it, but I see that this is a bad idea."

也许您不知道可以同时将多个规则应用于同一字段。您可以编写一个自定义规则,说明它不应以数字开头,然后将其与默认 minlength 规则一起使用。

$("#idform").validate({
    rules: { 
        account: { 
            validAccount: true, // <- custom rule to make sure does not start with number
            minlength: 4        // <- default rule (at least 4 characters entered)
        } 
    }
});

您可以根据需要使用任意数量的默认 and/or 自定义规则(方法)。另外,查看 the additional-methods.js file 了解更多信息。