/    Sign up×
Community /Pin to ProfileBookmark

Form Validation

I have some form validation done, but it’s not very good. How do i make sure that users can only use a-z, A-Z, 0-9. Would this help make sure my site is more secure. (there for users would not be able to use special characters)

Here is the code i have so far:

[code=php]// check that username is 5 characters or more
$username = $_POST[‘username’];
if (strlen($username) > 4){

}
else {
die

//email validation – i got this from a script

$email = $_POST[’email’];

if(eregi(“^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$”, $email)) {

}
else {
die[/code]

how can i make sure that the $username only can have a-z, A-Z, 0-9

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@jasonahouleJun 19.2007 — This will work.
[code=php]
if(preg_match('/W/', $username)) {
// found a non alphanumeric character.
}
[/code]
×

Success!

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