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. :)