Rev5
Rev5

Reputation: 7

Square brackets in php preg_match

I would like to in form I can enter only the letters of the alphabet, numbers, and square brackets, except for special characters. Whether the brackets to include special characters? I have such:

if (preg_match("/[^a-zA-Z0-9]+/", $characters))
{
    echo "Write only letters of alphabet and numbers!";
}

It detects letters and numbers correctly, does not pass the special characters but how to do that I can also enter the square brackets?

Upvotes: 0

Views: 5305

Answers (1)

sonofagun
sonofagun

Reputation: 535

As adeneo says,

if (preg_match('/[^a-zA-Z0-9\[\]]+/', $characters))
{
    echo "Letters, numbers and square brackets only!";
}

Upvotes: 2

Related Questions