-2

I'm using the Jqueryvalidation plugin and have added the pattern additional method but I can't seem to get my regular expression to work.

My regexp is as follows

/((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,64})/

Demo at jsfiddle

I'm not very good at these. Can anyone see what might be going wrong please?


UPDATE

Apologies, I really need to stop writing questions when I'm in a rush to be somewhere and make sure they are up to the minimum requirements. I am aware I did not include enough code so I will give this another go. Thank you for all the comments so far.

I am using the jqueryvalidation plugin to check user input before they can submit a form. I have added one of the "additional methods" from the plugins official GitHub which is is the pattern method. This method has the code as follows

$.validator.addMethod( "pattern", function( value, element, param ) {
    if ( this.optional( element ) ) {
        return true;
    }
    if ( typeof param === "string" ) {
        param = new RegExp( "^(?:" + param + ")$" );
    }
    return param.test( value );
}, "Invalid format." );

I then have the following code in my page

$().ready(function() {
  $("#admin-new-password-form").validate({
    rules: {
      password: {
        required: true,
        minlength: 6,
        maxlength: 64,
        pattern: "/((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,64})/"
      }
    },
    messages: {
      password: {
        required: "Please enter your new password",
        minlength: "Please enter at least 6 characters",
        maxlength: "Please enter no more than 64 characters",
        pattern: "Please enter a valid password"
      }
    }
  });
});

However, when I put in a password that should mark as correct as far as my regex is concerned (e.g. lu123LU@1 which should match 1 number, 1 lower case, 1 uppercase, and 1 character) it's still returning false. Here's a minimum example.

var value = "lu123LU@1";
var param = "/((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,64})/"

param = new RegExp( "^(?:" + param + ")$" );

alert(param.test( value ));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Please note that the code is from the plugin provided for the regex. If there's another way suggested for me to do it I'm all ears. I very rarely use regex and don't fully understand it's working myself so any suggestions for this rookie would be much appreciated. :)

Sparky
  • 98,165
  • 25
  • 199
  • 285
dpDesignz
  • 1,909
  • 10
  • 34
  • 70
  • Please read creating a [mcve]. – Blue Aug 30 '19 at 00:52
  • [Don't use `new RegExp`](https://stackoverflow.com/questions/17863066/why-do-regex-constructors-need-to-be-double-escaped/55793086#55793086) – CertainPerformance Aug 30 '19 at 00:53
  • @FrankerZ I'm sorry I thought that's what I did with the jsfiddle? – dpDesignz Aug 30 '19 at 00:54
  • @certaimperformance I can't control that part. That's part of the plugin. What do you suggest I use instead? As I said in not very good at regexp. – dpDesignz Aug 30 '19 at 00:55
  • 1
    Related: [For JavaScript related debugging questions can we do more to encourage including the required MCVE?](https://meta.stackoverflow.com/questions/388990/for-javascript-related-debugging-questions-can-we-do-more-to-encourage-including) A link doesn't count – CertainPerformance Aug 30 '19 at 00:55
  • @certaimperformance apologies, I was in a rush for a meeting. I will post a more complete question when I'm back in the office. – dpDesignz Aug 30 '19 at 01:05
  • @CertainPerformance I have updated my question. Is there any help you can suggest please? Even if it's just some links to some good tutorials on how I could do this better would be much appreciated. :) – dpDesignz Aug 30 '19 at 01:55
  • [This is the canonical](https://stackoverflow.com/questions/17863066/why-do-regex-constructors-need-to-be-double-escaped) – CertainPerformance Aug 30 '19 at 01:57
  • @CertainPerformance thank you. I now understand your comment about not using RegExp. Sorry, I didn't see that was a link before. I understand now that I should just pass the expression as itself, but the issue I'm having is because it's dynamically set by an external reference I can't hard code the expression (it may change between pages), but it's being passed into the function as a string. I tried using `eval()` to make it run which it did, but it's failing, so I think that's not the correct way to do it? I've been reading about `String.raw` but don't understand how to get it to work. – dpDesignz Aug 30 '19 at 02:30
  • Eg instead of passing `'foo\b \bbar'` pass ``String.raw`foo\b \bbar` `` – CertainPerformance Aug 30 '19 at 02:31
  • @CertainPerformance so I understand that, I think... So I tried changing `param = new RegExp( "^(?:" + param + ")$" );` to `param = String.raw`param`;` but I just get an error `TypeError: param.test is not a function`. I'm Googling furiously to try and understand all these. I think I need to re-learn javascript. It's been a fair few years. :) – dpDesignz Aug 30 '19 at 02:34
  • You still need to pass it to `new RegExp` – CertainPerformance Aug 30 '19 at 02:35
  • @CertainPerformance oh duh, thank you! Ok, I've done that. I've tried `param = new RegExp(String.raw`param`);` and `param = new RegExp(String.raw`${param}`);` and it's still returning false, but if I just set `param = /((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,64})/;` as raw content it returns true no worries? I'm very confused. – dpDesignz Aug 30 '19 at 02:38
  • Works for me https://jsfiddle.net/c4qyjohg/ – CertainPerformance Aug 30 '19 at 02:39
  • @CertainPerformance Right, that's being set there. But if you try pass it as a function value I can't get it to work. [Updated JSFiddle](https://jsfiddle.net/dpdesignz/m8426jca/16/) – dpDesignz Aug 30 '19 at 02:42
  • You didn't declare the string with `String.raw` when passing to `checkRegex`. `String.raw` is not a function (not really), it's just syntax – CertainPerformance Aug 30 '19 at 02:45
  • @CertainPerformance I really apprecaite your help. I'm sorry I don't seem to understand still. I thought that's what I was doing in line 3 with `param = String.raw`${param}`;`. The `param` is being passed in my function as a string, doesn't that make it a raw string to pass into the new regexp? the only difference I can see between our fiddles is I'm passing it in the function where you're declaring it inline? What am I missing that's blaringly obvious? – dpDesignz Aug 30 '19 at 02:50
  • No, like I said, `String.raw` is not really a function, you need to use it when *declaring a string*, it doesn't make any sense with an existing string – CertainPerformance Aug 30 '19 at 02:51
  • @CertainPerformance Oh! I understand now. Sorry. Ok this is where we're getting stuck. Because the string is being declared on the page and passed into this function which is in my file (I would like to not have to change that if possible. I want to use this function for multiple expressions.) is it possible to get it to work? I've tried `alert(checkRegex(String.raw\`/((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,64})/\`));` but that obviously didn't work either. – dpDesignz Aug 30 '19 at 02:55
  • Can you fix the backtick delimiters? That looks right – CertainPerformance Aug 30 '19 at 02:59
  • @CertainPerformance sorry, fixed. Have [updated my fiddle](https://jsfiddle.net/dpdesignz/m8426jca/19/) to reflect it as well. – dpDesignz Aug 30 '19 at 03:01
  • Slashes are delimiters for regex *literals* only. Strings only use `'`, `"`, or backtick delimiters. Since you're passing a string with backtick delimiters, just use backticks, not slashes https://jsfiddle.net/jgydutox/1/ – CertainPerformance Aug 30 '19 at 03:03
  • @CertainPerformance Oh! Thank you so much! You are a legend! – dpDesignz Aug 30 '19 at 03:05
  • Please be aware that the jQuery Validation Engine is not the same plugin as jQuery Validate. The fact that you found two different tags should have been a clue. Edited tags. – Sparky Aug 31 '19 at 04:07
  • @Sparky thank you. I was under the impression it was the same plugin but with a new name. Thanks for the information. :-) – dpDesignz Aug 31 '19 at 04:13

1 Answers1

0

Thanks to @CertainPerformance for their help. This is what fixed it for me

In my rules I changed this line

pattern: "/((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,64})/"

To this

pattern: String.raw`((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,64})`

And it is now working.


UPDATE

Thanks to @Arkni on the original GitHub issue tracker for pointing out that I just needed to escape my slashes for it to pass

pattern: "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\\W]).{6,64})"
dpDesignz
  • 1,909
  • 10
  • 34
  • 70