0

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']))
4
Contributors
7
Replies
12
Views
6 Years
Discussion Span
Last Post by klemme
1

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.

Votes + Comments
Well spotted, missed the comma.
0

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!

0

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

0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.