简体   繁体   English

正则表达式仅允许使用字母数字和至少两个字符

[英]Regex only allow alphanumeric and at least two characters

I am using PHP and have a string that I need to validate that must be... 我正在使用PHP,并且需要验证的字符串必须是...

  • Only AZ and 0-9 只有AZ和0-9
  • Not one character in its own 自己没有一个角色
  • Can not be just numbers on their own 不能仅仅靠数字
  • Can be just letters on their own 可以只是自己的字母

I have what I think is the correct regex for only Az and 0-9 which is... 我有我认为仅适用于Az和0-9的正确正则表达式是...

if(preg_match('/[^a-z_\-0-9]/i', $string))
{
  echo "not valid string";
}

but how do I add to this to also ensure that it is at least two characters long? 但是我该如何添加以确保它至少两个字符长?

I would look at 我会看

^[a-zA-Z0-9]{2,}$

start string, any character in a-z or A-Z or 0-9, matched two or more times, end string.

http://regex101.com/r/sN7tX4 http://regex101.com/r/sN7tX4

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

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