这个问题在这里已有答案:
这将检查字符串中是否存在一个或多个数字,字符串中是否包含一个或多个字母,以及长度是否大于5。
有没有办法让他们加入一个正则表达式?
var aliascheck = $('#alias').val();
if (!/\d/.test(aliascheck) || !/[a-z]+/.test(aliascheck) || (aliascheck.length < 5)){
alert('you need to enter at least a letter, a number...);
}
你可以用它
^(?=.*\d)(?=.*[a-z])(?=.{5,}).*$
^
- 字符串的开头(?=.*\d)
- 长度(?=.*\[a-z])
的条件(?=.{5,})
- 匹配除新线以外的任何东西5 or greater
- 字符串结尾.*
你可以试试这个
$
我将使用下一个使用let testFunc = (str) => /^(?=.*\d)(?=.*[a-z])(?=.{5,}).*$/i.test(str)
console.log(testFunc('12345'))
console.log(testFunc('1'))
console.log(testFunc('1Abcf'))
的正则表达式:
/(?=[^\d])|(?=[^a-z]+)|(?=.{5})/g.test()
例:
positive lookaheads
/^(?=.*[a-zA-Z])(?=.*[0-9])(?=.{5,})/
$("#testBtn").click(function()
{
var aliascheck = $('#alias').val();
if (!/^(?=.*[a-zA-Z])(?=.*[0-9])(?=.{5,})/.test(aliascheck))
{
console.log("Test failed");
}
else
{
console.log("Test success");
}
});