简体   繁体   English

javascript正则表达式警告无效的用户名

[英]javascript regex to warn invalid username

these are the conditions for a valid username这些是有效用户名的条件

  1. must contain az or AZ必须包含 az 或 AZ
  2. can contain 0-9 anywhere可以在任何地方包含 0-9
  3. must not allow any other characters不得允许任何其他字符

so a0 and 0a are valid user names just like username and UserName .所以a00a是有效的用户名,就像usernameUserName 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+aa+

please read the question completely, before putting down votes请在投票之前完整阅读问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM