0

Ive found some posts when people have specified how to add a method to the jquery validator, but Im getting an error. Below is my code:

// 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());
                }
            });
        });

But I am getting the following error:

Parser Error Message: "." is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.

Line 154: });

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

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

Line 157: });

Everything else works, except for the addition password checking.

Please can someone point out where and what I might be doing wrong, as Ive seen a post where the above checklower: true,checkupper: true, checkdigit: true was marked as the answer, so my assumption is that it should work, but mine is not :(

Thanks for help in advance.

AxleWack
  • 1,801
  • 1
  • 19
  • 51
  • I believe that error message refers to Razor view engine. If so, then you need to escape that `@` to be `@@`. – choz Sep 26 '18 at 14:53
  • Spot on @choz! I just posted the answer just before your comment. I do however feel that its not a dupe - the dupe you specified is to do with email checking, this is for password checking - even though the regex portion ultimately comes down to the same thing. – AxleWack Sep 26 '18 at 14:55
  • Glad you got it working. It is a dupe, but a different way of interpreting it. In the end, what you're looking for is actually in that post. – choz Sep 26 '18 at 14:58
  • Agreed. But I wouldnt have found that post as I was looking for password validation and was not aware of the @@ being the issue. But I get your point. – AxleWack Sep 26 '18 at 14:59

1 Answers1

-1

I literally just found the issue - I was tempted to delete this, but perhaps it helps someone else.

The issue lies in this code:

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

There was infact an error showing in my view, specifically on the @ sign. It needs to be a double @@, and then worked.

AxleWack
  • 1,801
  • 1
  • 19
  • 51
  • 1
    I dont mind users down voting, but why down vote, this is where the issue lies and solves the issue ?(Same applies to my question) – AxleWack Sep 26 '18 at 14:56