I'm dumb
I'm dumb

Reputation: 11

Using preg_match in php to verify username ( or others )

I read through the similar questions that already been asked but I still couldnn't get it right .

http://regexr.com/39b64

\S should return everything on the keyboard except space , tab and enter . ^$ should be a whole match as it starts from ^ and ends with $ .

There was a link that also uses something similar like the above with addition of {0,} which should be infinite letters but it doesn't work on regexr.com when I tested .

Another link suggested to remove the $ and replace it with \z but it doesn't work on regexr.com as well .

I'm planning to user preg_match to see whether a not the username enter is with all characters on the keyboard except space , tab and enter .

Username = "abcCD0123_"   valid

Username = "abcCD0123_!@#$%^&)_[]-=\',;/`~) valid

Username = "      abcd123~!#$@#%[];,.;'"   invalid

Username = "abcd123~!#$@#%[];,.;'    "   invalid

Username = "       abcd123~      !#$@#%[];,.;'     "   invalid

Something like that cause' I read about a question where someone suggested to do the verification matching on the php side instead of html side for security reasons .

edit : I tried ...

/^[\S]+$/
/^[\S]*$/
/^[\S]{0,}$/
/^[^\s\S]+$/
/^[^\s\S]*$/
/^[^\s\S]{0,}$/
/^[A-Za-z0-9~!@#$%^&*()_+{}|:"<>?`-=[]\;',./]+$/
/^[A-Za-z0-9~!@#$%^&*()_+{}|:"<>?`-=[]\;',./]*$/ 
/^[A-Za-z0-9~!@#$%^&*()_+{}|:"<>?`-=[]\;',./]{0,}$/

( something like this for this i can't remember cause' I modified a lot of times on this one )

Upvotes: 0

Views: 324

Answers (3)

Braj
Braj

Reputation: 46861

You need to use m (PCRE_MULTILINE) modifier if you want to use ^ and $

When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end.

Here is demo to check for any string/line that contains non white space and length is in between 8 to 50

^[^\s]{8,50}$

Online demo

OR

^\S{8,50}$

Online demo

Sample code: (Focus on m modifier)

$re = "/^[^\\s]{8,50}$/m";
$str = "...";

preg_match_all($re, $str, $matches);

Upvotes: 2

enrico.bacis
enrico.bacis

Reputation: 31514

You can just check that the line is composed by only non-space characters (demo):

/^\S+$/

Strings with multiple lines

The regex assumes that you are checking a single username at time (what you probably want to do in your code). But as shown in the demo and as described by user3218114 in his answer, if you have a multiple line string, you need to use the m flag to allow ^ and $ to match also for begin end of each line (otherwise it will just match begin/end of the string). This is probably why your tests weren't working.

/^\S+$/m

Upvotes: 2

MrTux
MrTux

Reputation: 34002

Based on your examples I suppose you want to have something like that:

/^[a-z0-9_!@#$%&^()_\[\]=\\',;\/~`-]+$/i

It's one character group [] which contains all allowed characters. Note, however, that one cannot just put all chars in there, some characters have special meanins in regexp and must be escaped by \ ([,],(),),/ and \ itself). You also have to be careful where to put -. In the case of a-z it means all characters between a and z (including a and z). That's why I put the - char itself at the end.

To match really everything except white-space use /^\S+$/

Upvotes: 0

Related Questions