简体   繁体   English

用于用户登录验证的正则表达式

[英]Regex for user login verification

I am trying to use regex to check if a user name is considered valid. 我正在尝试使用正则表达式来检查用户名是否被视为有效。 For a user name to be valid it must be between 2 and 10 characters long. 为了使用户名有效,它必须在2到10个字符之间。 It must start with a lower case letter and it must be made up of only lowercase letters, upper case letters or digits. 它必须以小写字母开头,并且只能由小写字母,大写字母或数字组成。

Here is what I have so far 这是我到目前为止的

return preg_match("/^[a-z]([a-zA-Z0-9]{2,10})$/", $name);

For consistency, your capture expression should include your whole username (in your case you are skipping the first letter). 为了保持一致性,捕获表达式应包含整个用户名(在这种情况下,您将跳过第一个字母)。 You've also noticed you only need to use {1,9} for the second half: 您还注意到下半部分只需要使用{1,9}

^([a-z]{1}[a-zA-Z\d]{1,9})$
  • this is matching one lowercase letter to begin with, followed by between 1 and 9 a-zA-Z0-9 s afterwards, and capturing the whole username. 这是匹配一个开头的小写字母,然后是1到9个a-zA-Z0-9 ,然后捕获整个用户名。

Modifiers (if required): mg (multi-line, match-all [for when testing with a block of multiple lines]) 修饰符(如果需要): mg (多行,全部匹配[用于测试多行块时])

Demo: http://regex101.com/r/qW6kN5 演示: http//regex101.com/r/qW6kN5

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

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