How to trigger a custom validation if value exists in input field using jquery validate?

I have password field(option) its not required to validate while submit form. But if i am entered data in the password field it must test the password strength custom validation. Please help me how to fix this issue. It validate only if password field is not empty.

My Code Here:

js.validator.addMethod("regx", function(value, element, regexpr) { 
return regexpr.test(value);
}, "Password must contain one lower case letter,upper case letter,digit and special character ex:aaEs#s1@.");

 js('#forms').validate({
  rules: {
 password:{ 
        regx: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{5,20}$/ 
      },         

  }
});

Answers


Add this.optional(element) to your conditional check to make this field optional.

js.validator.addMethod("regx", function(value, element, regexpr) { 
    return  this.optional(element) || regexpr.test(value);
}, "Password must contain one lower case letter,upper case letter,digit and special character ex:aaEs#s1@.");

Need Your Help

Aligning floating elements to bottom of div

html css

Trying to float one ul left and one ul right and keeping them to bottom of div with no success