简体   繁体   English

用于用户名验证的JavaScript正则表达式

[英]JavaScript regex for username validation

I tried with many patters for username in my Angular5 application. 我在Angular5应用程序中尝试了很多模式来用作用户名。 But didn't get a suitable solution for my requirement. 但是没有得到适合我要求的解决方案。

The Rules are 规则是

Minimum 6 characters 至少6个字符
Only numbers are not allowed at least one character should be there 只允许输入数字,至少应有一个字符
No special characters allowed except _ 除_之外,不允许使用特殊字符
No space allowed 不允许空间
Character only is allowed 只能输入字符

I tried with /^[a-zA-Z0-9]+([_]?[a-zA-Z0-9])*$/ 我尝试使用/^[a-zA-Z0-9]+([_]?[a-zA-Z0-9])*$/

/^[a-zA-Z0-9][a-zA-Z0-9_]*[a-zA-Z0-9](?<![_\s\-]{6,}.*)$/

You can use the following regex: 您可以使用以下正则表达式:

^(?=[a-z_\d]*[a-z])[a-z_\d]{6,}$

in case insensitive mode as tested on regex101: demo 在regex101上测试的不区分大小写的模式下: 演示

Explanations: 说明:

  • ^ anchor for the beginning of the string ^字符串开头的锚点
  • $ anchor for the end of the string $字符串结尾的锚点
  • (?=[a-z_\\d]*[az]) to force the presence of at least one letter (?=[a-z_\\d]*[az])强制至少存在一个字母
  • [a-z_\\d]{6,} implement the at least 6 char constraint [a-z_\\d]{6,}实现至少6个char约束

You can try this regex: 您可以尝试以下正则表达式:

^[a-zA-Z0-9_]{5,}[a-zA-Z]+[0-9]*$
  • [a-zA-Z0-9_]{5,} to match at least five alphanumerics and the underscore [a-zA-Z0-9 _] {5,}至少匹配五个字母数字和下划线
  • [a-zA-Z]+ to have at least one letter [a-zA-Z] +至少有一个字母
  • [0-9]* to match zero to any occurrence of the given numbers range [0-9] *使零与给定数字范围内的任何匹配

Hope this helps. 希望这可以帮助。

Yes this is fine for me. 是的,这对我很好。 Thanks. 谢谢。 /^[a-zA-Z0-9][a-zA-Z0-9_]*[a-zA-Z0-9](?<![-?\\d+\\.?\\d*$]{6,}.*)$/

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

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