[英]javascript regex to warn invalid username
these are the conditions for a valid username这些是有效用户名的条件
so a0
and 0a
are valid user names just like username
and UserName
.所以
a0
和0a
是有效的用户名,就像username
和UserName
。 i need a regex for this.我需要一个正则表达式。
the issue optionally allowing 0-9该问题可选地允许 0-9
UPDATE the problem with regex like /\\w+/i
is it allows username like 000
but in my case it must contain an alphabet and not any special characters更新像
/\\w+/i
这样的正则表达式的问题是它允许像000
这样的用户000
但在我的情况下它必须包含一个字母而不是任何特殊字符
You don't know how to compose lookahead ?你不知道如何编写lookahead? just use
只是使用
var isValid = /[a-zA-Z]/.test(username) && /^[a-zA-Z\d]$/.test(username);
^^^^^^^^^ at last one char
^^^^^^^^^^^^^only char & digits
You want to learn lookahead ?你想学习前瞻吗? Just use:
只需使用:
var isValid = /^(?=.*[a-z])[a-z\d]+$/gmi.test(username);
也许是这样的:
/^[a-zA-z0-9]*[a-zA-z][a-zA-z0-9]*$/
简单的解决方案:
/^(\d*[a-zA-Z]+\d*)+$/
the working regex is /^([0-9]+[a-zA-Z]|[a-zA-Z]+[0-9]|[a-zA-Z])+$/
.工作正则表达式是
/^([0-9]+[a-zA-Z]|[a-zA-Z]+[0-9]|[a-zA-Z])+$/
。
it does match 0a
a0
aaa
它确实匹配
0a
a0
aaa
but will not match 000
or +a
or a+
但不会匹配
000
或+a
或a+
please read the question completely, before putting down votes请在投票之前完整阅读问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.