Forum Moderators: coopster

Message Too Old, No Replies

"Simple" preg match issue of a PHP dummy

Username errors

         

LawGen

11:22 pm on Feb 20, 2010 (gmt 0)

10+ Year Member



I am attempting to fix a problem with a "canned" PHP script which inhibits spaces in usernames. As the subject (and the script will prove out), I am a complete "dummy" when it comes to this stuff.

All I want, is to allow individuals the opportunity to have spaces in their username. Thats it, but I can't seem to figure this stuff out. Have already spent several hours researching and trying to come up with a solution.

Any help you can provide would be greatly appreciated.

SEE BELOW

## Check the username characters
if (!preg_match('/^[\w-]+$/', $data['username'])){return "username_chars";

rocknbil

2:49 am on Feb 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard LawGen, first,

[\w-]

Within a class, a dash usually means a range, as in a-z is all letters. \w may not suffice, as it is a word character, not always specifically letters.

All I want, is to allow individuals the opportunity to have spaces in their username.


The way I would go about this is a not - as in, anything not a letter, number, dash, or space. inside a class, ^ as the first character means "anything not these." In the regular course of a regex, it means "start of string."

if (preg_match('/[^a-z0-9\s\-]+/i', $data['username'])) { echo "ERROR"; }
else { echo "OK"; }

I left the dash in there in case you want it, as you can see it's escaped. Translated, the whole thing is "if the string contains anything not a case-insensitive (i modifier) letter, number, space, or dash, error, otherwise, OK."

You can skip the "else" if "echo error" actually exits.

LawGen

3:23 am on Feb 21, 2010 (gmt 0)

10+ Year Member



rocknbil-

Thank you for the reply and welcome. I have spent hours trying to get this darn thing to work (but to no avail).

Surprisingly, after spending 5 hours reading up on PHP, I actually understand what you are telling me below...I get it. Would
[^a-z0-9
only allow for lowercase letters?

I tried the code in Cpanel and am still receiving the "invalid charachters" response. I am at a loss. Perhaps i am missing another string that needs to be changed?

[\w-]

Within a class, a dash usually means a range, as in a-z is all letters. \w may not suffice, as it is a word character, not always specifically letters.

The way I would go about this is a not - as in, anything not a letter, number, dash, or space. inside a class, ^ as the first character means "anything not these." In the regular course of a regex, it means "start of string."

if (preg_match('/[^a-z0-9\s\-]+/i', $data['username'])) { echo "ERROR"; }
else { echo "OK"; }


I left the dash in there in case you want it, as you can see it's escaped. Translated, the whole thing is "if the string contains anything not a case-insensitive (i modifier) letter, number, space, or dash, error, otherwise, OK."

Readie

4:50 am on Feb 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Would
[^a-z0-9\s\-]+/i

only allow for lowercase letters?

matches anything that is not:
- letters (lower case or upper case)
- numbers
- whitespace
- dashes

so in

if(preg_match('/[^a-z0-9\s\-]+/i', $data['username'])) {
echo "ERROR";
} else {
echo "OK";
}


It will shout ERROR if anything other than letters, numbers, whitespace or dashes is entered

rocknbil

6:44 pm on Feb 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Would '/[^a-z0-9]+/'
only allow for lowercase letters?


As bolded above, YES, but with the i modifier,

'/[^a-z0-9]+/i'

it makes it case-InsEnSitIVe.