Jquery验证密码强度大写和小写

时间:2012-03-22 20:46:18

标签: javascript jquery regex jquery-validate case-sensitive

$.validator.addMethod("validpassword", function(value, element) {
    return this.optional(element) ||
        /^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d])(?=.*[\W]).*$/i.test(value);
}, "The password must contain a minimum of one lower case character," +
           " one upper case character, one digit and one special character..");

上述正则表达式不区分大写和小写。怎么了?

1 个答案:

答案 0 :(得分:9)

删除i标志。它使RegEx 不区分大小写。另外,将[\W]扩展为[\W_]

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

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