gpt4 book ai didi

javascript - jQuery 验证插件 this.optional 不是函数

转载 作者:行者123 更新时间:2023-11-30 15:02:50 26 4
gpt4 key购买 nike

我创建了一个自定义验证。在文档中我找到了方法 addMethod()

我的例子:

Template.Login.onRendered(() => {
jQuery.validator.addMethod('strongPassword', (value, element) => {
return this.optional(element)
|| value.length >= 6
&& /\d/.test(value)
&& /[a-z]/i.test(value);
}, 'Your password must be at least 6 characters long and contain at least one number and one char.');

$('.login-form').validate({
rules: {
password: {
required: true,
strongPassword: true
}
}
});
});

我得到错误:

_this.optional is not a function. Exception occurred when checking element , check the 'strongPassword' method.

这是我的问题吗?

最佳答案

问题与this有关。当您使用箭头函数时(那里有 2 种情况),this 指的是上面一个的 div 上下文,在您的情况下是 this,其中 onRendered 已被调用。将 arrow function 更改为简单函数以获取适当的 this 上下文。

jQuery.validator.addMethod('strongPassword', function (value, element) {
return this.optional(element)
|| value.length >= 6
&& /\d/.test(value)
&& /[a-z]/i.test(value);
}, 'Your password must be at least 6 characters long and contain at least one number and one char.');

关于javascript - jQuery 验证插件 this.optional 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46261196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com