Javascript 密码不应包含用户的帐户名或用户全名中超过两个连续字符的部分

Javascript Password should not contain user's account name or parts of the user's full name that exceed two consecutive characters

本文关键字:用户 两个 字符 连续 全名 包含 密码 Javascript 中超      更新时间:2023-09-26

我需要实现客户端密码验证,以便密码不应包含用户的帐户名或用户全名中超过两个连续字符的部分。

我在客户端公开用户名和全名。但到目前为止,我无法弄清楚正则表达式或任何其他在客户端实现的方法。

username: test20@xyz.com
password: Usertest123 --> this should fail the validation since it contains "test" in both password and username.

我只能想到这一点:

var name = "test20@xyz", password = "usertest123"
var partsOfThreeLetters = name.match(/.{3}/g).concat(
                           name.substr(1).match(/.{3}/g),
                           name.substr(2).match(/.{3}/g) );
new RegExp(partsOfThreeLetters.join("|"), "i").test(password); // true

但我不认为正则表达式是这里的合适工具,因为它需要转义等。你最好使用一个简单的substr/indexOf算法(参见JavaScript不区分大小写的字符串比较,如何检查一个字符串是否包含JavaScript中的子字符串?)。

如果你在 TS 中需要它,你可以键入:`const threePart = email.match(/.{3}/g) ||[];

const allThreeParts = threeParts.concat(email.slice(1).match(/.{3}/g) ?? [], email.slice(2).match(/.{3}/g) ?? []);
return new RegExp(allThreeParts.join("|"), "i").test(password);

'