向JQuery验证器添加其他密码检查

时间:2018-09-26 14:46:59

标签: jquery regex

当人们指定了如何向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());
                }
            });
        });

但是我遇到以下错误:

  

解析器错误消息:“。”在代码块的开头无效。   仅标识符,关键字,注释,“(”和“ {”是有效的。

     

第154行:});

     

第155行:$。validator.addMethod(“ pwcheck”,函数(值){

     

第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被标记为答案,所以我认为应该可以,但我的却不行: (

谢谢您的帮助。

1 个答案:

答案 0 :(得分:-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);
            });

在我看来,实际上是一个错误,特别是@符号。它必须是双@@,然后才能工作。