Hi guys,

It is working fine, and only allowing letters and numbers.

I want to a feature, so that it has to be between 3-20 charachters/numbers long.

if (!preg_match("/^[A-Za-z0-9]+$/i", $_POST['brugernavn'])){
$dosomething = '';
}

This is not working:

if (!preg_match("/^[A-Za-z0-9]{3-20}+$/i", $_POST['brugernavn']))

Recommended Answers

All 7 Replies

It should be:

if (!preg_match("/^[A-Za-z0-9]{3, 20}$/i", $_POST['brugernavn']))

The {3, 20} part checks if the length is between 3 and 20 characters.

commented: Well spotted, missed the comma. +13

Okay thanks,

I am def not god at using preg_match, so another ? here:

You wrote that i shuld remove the plus sign. What excactly does the plus sign means? When it is there, it does something i guess?

And does theese two means the same:

if(!preg_match('/^[a-zA-Z0-9]{3,20}$/', $_POST['efternavn']))
if(!preg_match("/^[a-z\d'\.\-]{3,20}$/i", $_POST['brugernavn']))

I have searched the net for different solutions, and theres a ton of different approaches, for the untrained eye at least.

If someone can bother, and have the interest, I would be interested in reading a reply with a "small analysis", og the two expressions above.

I thinkk its much easier, since im not very familiar with preg_match, to use the first one, since the second one seems to provide "letters and stuff", to achieve the same meaning?

Hope someone have the time to break it down for me,

Thanks, later!

Hi klemme

in regular expression the "+" referes Matches the preceding pattern element one or more times.

\d(or)[0-9] a digit(0-9).
i Matches not case sensitive
[a-z] Lowercase letters
[A-Z] Uppercase letters
[A-Za-z] Both lowercase and uppercase letters

If u want to match any special characters, need to add \ in fornt of the special character.

U used \.\-. It mean that u for find matching for both "." and "-"

I think now u can understand b/w the two commands....

if(!preg_match('/^[a-zA-Z0-9]{3,20}$/', $_POST))
if(!preg_match("/^[a-z\d'\.\-]{3,20}$/i", $_POST))

If u have any doubts, ask me....

In addition, just to help you get started: the website regular-expressions.info has a lot of usable info. He also has two tools which you can trial, RegExBuddy and RegExMagic. These help you build and understand regex expressions.

Thanks so much,

Nice breakdown of the question. I knew what [azAZ09] meant, but didnt know that the letter d, also is meaning numbers, god the guy inventing these pattern structure would have been mad, though they are powerfull :-)

Ill look at the site and mark the thread as solved,

Cheers
Klemme

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.