开发者

Javascript regular expression for "at least X characters, have a capital letter and a number"

开发者 https://www.devze.com 2023-03-06 01:17 出处:网络
I have a开发者_运维技巧 regular expression for a password field which I know works perfectly i.e. Must be at least 6 characters in length, have a capital letter, a number and can contain special chara

I have a开发者_运维技巧 regular expression for a password field which I know works perfectly i.e. Must be at least 6 characters in length, have a capital letter, a number and can contain special characters. When I try to apply this regex within Javascript it doesn't seem to validate. My Javascript function is below.

function (word) {
    var weakRegEx = new RegExp('(?=^.{6,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$');
    var result = weakRegEx.test(word);
    return result;


Since you're writing a string literal, you need to escape the \ characters.

You should use a regex literal instead:

var weakRegEx = /(?=^.{6,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/;
0

精彩评论

暂无评论...
验证码 换一张
取 消