我需要实现客户端密码验证,以便密码不应包含用户的帐户名或超过两个连续字符的用户全名部分。
我在客户端公开用户名和全名。但到目前为止,我无法弄清楚在客户端实现的正则表达式或任何其他方法。
例子
username: test20@xyz.com
password: Usertest123 --> this should fail the validation since it contains "test" in both password and username.
我需要实现客户端密码验证,以便密码不应包含用户的帐户名或超过两个连续字符的用户全名部分。
我在客户端公开用户名和全名。但到目前为止,我无法弄清楚在客户端实现的正则表达式或任何其他方法。
例子
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 中的子字符串?) .