0

I am using jQuery validation plugin and I want to create a password strength regex using data-rule-pattern to validate the field and can't make it work at all.

I want the password to have minimum:

  • 8 characters
  • 1 uppercase letter
  • 1 number

So far I have:

<p class="controls">
<input data-rule-pattern="(^$)|([a-z][A-Z]{1}[0-9]{1}$)" data-msg-pattern="Password does not meet the minimum requirments." id="password" type="password" data-rule-required="true" value="" />
</p>

The other simple validations are working ok, but can't get this one done. Can anyone help please with some ideas? Thanks!

Sparky
  • 98,165
  • 25
  • 199
  • 285
Dan
  • 715
  • 8
  • 12

2 Answers2

2

You can Apply your regular expression to the element with with customer addMethod like this

this is my custom addMethod that add regular expression

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

this is my rules method that attach that custom addMethod to my element

 $('input[name="password"]').rules('add', {
                    PASSWORD: true
                });

this is my element

  <input id="password" type="password" class="w-input"  data-rule-required="true" name="password" placeholder="Password"  />

OR You Can Directly Assign Your Custom Method To Your Element

<input id="password" type="password" class="w-input" data-rule-PASSWORD="true"  data-rule-required="true" name="password" placeholder="Password" data-rule-password ="true" />
Hope It Will work For You
1

Did you think to include additional-methods.js?
Because pattern rule is not included in core jquery.validate.js!

ebaynaud
  • 682
  • 6
  • 15