fightstarr20
fightstarr20

Reputation: 12598

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...

I have what I think is the correct regex for only A-z and 0-9 which is...

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?

Upvotes: 2

Views: 3360

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 28993

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

Upvotes: 1

Related Questions