【问题标题】:Adding additional password checking to JQuery validator [duplicate]向 JQuery 验证器添加额外的密码检查 [重复]
【发布时间】:2018-09-26 14:46:58
【问题描述】:

当人们指定如何向 jquery 验证器添加方法时,我发现了一些帖子,但我得到了一个错误。以下是我的代码:

// Validation
        $(function () {
            // Validation
            $.validator.addMethod("checklower", function (value) {
                return /[a-z]/.test(value);
            });
            $.validator.addMethod("checkupper", function (value) {
                return /[A-Z]/.test(value);
            });
            $.validator.addMethod("checkdigit", function (value) {
                return /[0-9]/.test(value);
            });
            $.validator.addMethod("pwcheck", function (value) {
                return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) && /[a-z]/.test(value) && /\d/.test(value) && /[A-Z]/.test(value);
            });

            $("#smart-form-register").validate({
                // Rules for form validation
                rules: {
                    email: {
                        required: true,
                        email: true
                    },
                    emailConfirm: {
                        required: true,
                        email: true,
                        equalTo: '#email'
                    },
                    password: {
                        required: true,
                        minlength: 4,
                        maxlength: 20,
                        checklower: true,
                        checkupper: true,
                        checkdigit: true
                    },
                    passwordConfirm: {
                        required: true,
                        minlength: 4,
                        maxlength: 20,
                        equalTo: '#password'
                    },
                    firstname: {
                        required: true
                    },
                    lastname: {
                        required: true
                    },
                    gender: {
                        required: false
                    },
                    DOB: {
                        required: true
                    },
                    SelectedBU: {
                        required: true
                    },
                    terms: {
                        required: false
                    }
                },

                // Messages for form validation
                messages: {
                    email: {
                        required: 'Please enter your email address',
                        email: 'Please enter a VALID email address'
                    },
                    emailConfirm: {
                        required: 'Please enter your email address one more time',
                        email: 'Please enter a VALID email address',
                        equalTo: 'Please enter the same email address as above'
                    },
                    password: {
                        required: 'Please enter your password',
                        pwcheck: 'Check that your password works',
                        checklower: "Need atleast 1 lowercase alphabet",
                        checkupper: "Need atleast 1 uppercase alphabet",
                        checkdigit: "Need atleast 1 digit"
                    },
                    passwordConfirm: {
                        required: 'Please enter your password one more time',
                        equalTo: 'Please enter the same password as above'
                    },
                    birthdate: {
                        required: 'Please select a birth date',
                    },
                    SelectedBU: {
                        required: 'Please select a business unit',
                    },
                    terms: {
                        required: 'You must agree with Terms and Conditions'
                    }
                },

                // Do not change code below
                errorPlacement: function (error, element) {
                    error.insertAfter(element.parent());
                }
            });
        });

但我收到以下错误:

解析器错误消息:“。”在代码块的开头无效。 只有标识符、关键字、cmets、"(" 和 "{" 是有效的。

第 154 行:});

第 155 行:$.validator.addMethod("pwcheck", function (value) {

第 156 行:返回 /^[A-Za-z0-9\d=!-@._]$/.test(value) && /[a-z]/.test(value) && /\d/.test(value) && /[A-Z]/.test(value);

第 157 行:});

除了额外的密码检查之外,其他一切都有效。

请有人指出我可能做错的地方和地方,因为我看过一篇帖子,上面的checklower: true,checkupper: true, checkdigit: true 被标记为答案,所以我的假设是它应该有效,但我的不是:(

提前感谢您的帮助。

【问题讨论】:

  • 我相信错误消息是指 Razor 视图引擎。如果是这样,那么您需要将 @ 转义为 @@
  • 关注@choz!我刚刚在您发表评论之前发布了答案。然而,我确实觉得这不是一个骗局——你指定的骗局与电子邮件检查有关,这是用于密码检查——即使正则表达式部分最终归结为同一件事。
  • 很高兴你能成功。这是一个骗局,但解释它的方式不同。最后,您要查找的内容实际上在该帖子中。
  • 同意。但是当我正在寻找密码验证并且不知道 @@ 是问题时,我不会找到该帖子。但我明白你的意思。

标签: jquery regex


【解决方案1】:

我确实发现了这个问题 - 我很想删除它,但也许它对其他人有帮助。

问题在于这段代码:

$.validator.addMethod("pwcheck", function (value) {
                return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) && /[a-z]/.test(value) && /\d/.test(value) && /[A-Z]/.test(value);
            });

在我的视图中实际上显示了一个错误,特别是在 @ 符号上。它需要是双@@,然后才能工作。

【讨论】:

  • 我不介意用户投票,但为什么要投票,这是问题所在并解决了问题?(同样适用于我的问题)
猜你喜欢
  • 2012-04-15
  • 1970-01-01
  • 2019-08-08
  • 2016-10-30
  • 1970-01-01
  • 2016-05-24
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
相关资源
最近更新 更多