/    Sign up×
Community /Pin to ProfileBookmark

Finding special characters in a username

I’m writing a script to validate a username for a registration page. I want to alert the user if they have any special characters in their username, and I am using the php preg_match() function to test for special characters. I’m not very good with regular expressions, does anyone know a good regular expression to match all special characters wherever they may be in the username string? Also is there a better way to do this than using preg_match?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiAug 22.2009 — It is easier and more efficient to validate against an allowed set of characters rather than trying to exclude everything individually. For example, if you only want to allow letters, numbers and underscores:

[code=php]$valid = preg_match('/[a-z0-9_]+/i', $username);[/code]
Copy linkTweet thisAlerts:
@adamscott421authorAug 22.2009 — Thanks for the tip that really helped. What do the forward slashes and the 'i' mean in the example regex you gave?
×

Success!

Help @adamscott421 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.19,
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,
)...