1

Topic: Regular expression for usernames

I created a script to allow users to upload their images and i create a new folder on my server for every user.
i noticed that punbb allows users to register with names containing "forbidden" characters (!*#) and then i can't create a folder with those names, so i'd like to check usernames during registration so that they use only chars allowed by my (linux) hosting

any suggestion is appreciated, i know i have to use something like preg_match, but i don't know how...
thank you smile

Re: Regular expression for usernames

You could try something like this:

<?php
if(strpos($username,'*') || strpos($username,'!') || strpos($username,'#'))
    {
    error("Your name contains invalid characters!");
    }
?>

Re: Regular expression for usernames

you could use something along the lines of:

function valid_username($username) {
    return preg_match('/[^a-zA-Z0-9]/', $username) == 0;
}

That will only allow alphanumeric characters.