0

I know this question has been asked to death, but none of the answers seem to be working, so don't know if its the plugin that's the problem or my code. I'm using the JQuery Validate plugin and have checked the documentation of the regex and am certain I have everything correct. However when I type in test1234 or TEST1234 as the password the validate plugin seems to accept this as password when I've explicitly said I want 1 uppercase, 1 lowercase, 1 digit and 8-12 characters in my regex. Ironically when I type in a 7 character password it triggers off, if I only use digits and match 8 characters it triggers off.

Can someone double check the code I have below is OK? I have also tried the following:

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,12}$
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,12}$



<!--
(                   # Start of group
(?=.*\d)        #   must contain at least one digit
(?=.*[A-Z])     #   must contain at least one uppercase character
(?=.*\W)        #   must contain at least one special symbol
   .            #   match anything with previous condition checking
{8,12}          #   length at least 8 characters and also maximum of 12
)               #   End of group

 -->


 $().ready(function () {

$.validator.addMethod("PASSWORD", function (value, element) {
  return this.optional(element) || /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}$/i.test(value);
}, "Passwords are 8-12 characters with uppercase letters, lowercase letters and at least one number.");

// Validate signup form on keyup and submit
$("#cp").validate({
    rules: {
      new_pass1: "required PASSWORD",
      new_pass2: {required: true, equalTo: "#new_pass1"}
  },
});
});
Sparky
  • 98,165
  • 25
  • 199
  • 285
JK36
  • 853
  • 2
  • 14
  • 37

1 Answers1

0

Just remove i flag. 'i' flag enables case-insensitive search. So it should be:

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}$/.test(value)

Check this: https://jsfiddle.net/djanym/0xrkgnsL/12/

Djanym
  • 332
  • 1
  • 4
  • 13