/    Sign up×
Community /Pin to ProfileBookmark

bad REGEX pattern

Hi im trying to get my regular expression to work, im pants at it and when i look at the patterns my brain melts ?

I want my regEX to only accept alpha numberical characters underscores [COLOR=”Red”][/COLOR] and [COLOR=”red”].[/COLOR] characters and if it doesnt to warn them this should work from what i can see but its not working

check here [url]http://www.liquidfists.com/index2.php?page=signup[/url] and it not achieving the desired results.

[code=php]
//CHECK USERNAME AND PASSWORD FOR VALID CHARICTERS
if (preg_match(‘/^[w.-]+$/’, $user_name) {

$login_warn_msg=’You have entered invalid characters in the username field.’;
$login_warn='<p class=”warning”>’.$login_warn_msg.'</p>’.”n”;
$login_warn.=$login_form;

echo $login_warn;
exit();
}
[/code]

for example if i try and register the username testperson it says that i have invalid characters even though i dont because they all match alpha numberical characters underscores [COLOR=”Red”][/COLOR] and [COLOR=”red”].[/COLOR] characters

anyhelp would be greatly appreciated.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@tfk11Nov 06.2008 — The . character has special meaning within character classes.

http://www.regular-expressions.info/dot.html

Your expression as it is will match pretty much anything. Escape the dot and you should be in good shape.

preg_match('/^[w.-]+$/', $user_name)
Copy linkTweet thisAlerts:
@NogDogNov 06.2008 — The . character has special meaning within character classes.

http://www.regular-expressions.info/dot.html

Your expression as it is will match pretty much anything. Escape the dot and you should be in good shape.

preg_match('/^[w.-]+$/', $user_name)[/QUOTE]


Actually, within a bracketed character class, the "." has no special meaning.

[indent]"All non-alphanumeric characters other than , -, ^ (at the start) and the terminating ] are non-special in character classes, but it does no harm if they are escaped." (from http://php.net/manual/en/regexp.reference.php)[/indent]
Copy linkTweet thisAlerts:
@NogDogNov 06.2008 — Hi im trying to get my regular expression to work, im pants at it and when i look at the patterns my brain melts ?

I want my regEX to only accept alpha numberical characters underscores [COLOR="Red"]-[/COLOR] and [COLOR="red"].[/COLOR] characters and if it doesnt to warn them this should work from what i can see but its not working

check here http://www.liquidfists.com/index2.php?page=signup and it not achieving the desired results.

[code=php]
//CHECK USERNAME AND PASSWORD FOR VALID CHARICTERS
if (preg_match('/^[w.-]+$/', $user_name) {

$login_warn_msg='You have entered invalid characters in the username field.';
$login_warn='<p class="warning">'.$login_warn_msg.'</p>'."n";
$login_warn.=$login_form;

echo $login_warn;
exit();
}
[/code]


for example if i try and register the username testperson it says that i have invalid characters even though i dont because they all match alpha numberical characters underscores [COLOR="Red"]-[/COLOR] and [COLOR="red"].[/COLOR] characters

anyhelp would be greatly appreciated.[/QUOTE]


Your if condition is true if the string only contains valid characters, so you want to either negate it or compare it to FALSE:
[code=php]
if( ! preg_match(...)) {

// ...or... //
if(preg_match(...) == false) {
[/code]
Copy linkTweet thisAlerts:
@shakeukauthorNov 06.2008 — thank you, that appears to be working i forgot about the ! symbol meaning to not match, i only just started working with php again after about 5-6 months so my novice skills have probably been demoted to outright begginer lol.
×

Success!

Help @shakeuk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...