普通的前pression验证的用户名 [英] Regular expression to validate username

查看:148
本文介绍了普通的前pression验证的用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个常规的前pression验证对这些标准的用户名:


  1. 只包含的字母的角色,
    下划线的和的

  2. 下划线和点不能在结束的或
    启动的用户名(如 _username / 用户名_ / 。用户名 / 的用户名。)。

  3. 下划线和点不能的相邻的(例如 user_.name )。

  4. 下划线或点不能多次使用的连续的(例如 user__name / user..name )。

  5. 字符数必须是20 8之间。

这是我到目前为止已经完成;这听起来它强制规定的所有的标准,但在第5规则的。我不知道该怎么第五规则添加到这一点:

  ^ [A-ZA-Z0-9] +([._] [A-ZA-Z0-9] +)* $


解决方案

  ^(?=。{8,20} $)(?![_])(?!。 ?* [_] {2})[A-ZA-Z0-9 ._] +(小于![_])$
 └─────┬────┘└───┬──┘└─────┬─────┘└─────┬─────┘ └───┬───┘
       ││││无_或。最后
       ││││
       │││允许的字符
       │││
       ││无__或_。或._或里面..
       ││
       │无_或。初
       │
       用户名8-20个字符

I'm trying to create a regular expression to validate usernames against these criteria:

  1. Only contains alphanumeric characters, underscore and dot.
  2. Underscore and dot can't be at the end or start of a username (e.g _username / username_ / .username / username.).
  3. Underscore and dot can't be next to each other (e.g user_.name).
  4. Underscore or dot can't be used multiple times in a row (e.g user__name / user..name).
  5. Number of characters must be between 8 to 20.

This is what I've done so far; it sounds it enforces all criteria rules but the 5th rule. I don't know how to add the 5th rule to this:

 ^[a-zA-Z0-9]+([._]?[a-zA-Z0-9]+)*$

解决方案

^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$
 └─────┬────┘└───┬──┘└─────┬─────┘└─────┬─────┘ └───┬───┘
       │         │         │            │           no _ or . at the end
       │         │         │            │
       │         │         │            allowed characters
       │         │         │
       │         │         no __ or _. or ._ or .. inside
       │         │
       │         no _ or . at the beginning
       │
       username is 8-20 characters long

这篇关于普通的前pression验证的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆