Page 1 of 1

How to control what is used for a username at signup

Posted: 2016-03-29 03:40
by shasta59
Using Appgini 5.50

Have you ever wanted to control what is used as a username when a new user creates an account? For example, if you wish to not allow 'Superman vs Batman' as a user name then just do the following:

Open inc.Function.php in the admin folder of your app.

Then look for function is_allowed_username($username){ - should be around line 662

Directly below $username = trim(strtolower($username));
Create a new line with the following:

Code: Select all

if(preg_match('/superman vs batman/i', $username)) return false;
(The “i” after the pattern delimiter indicates a case-insensitive search )

You can put in whatever you wish to prevent being used. In this case, once someone types in whatever you wish to prevent it will come up with a warning and not allow the username. If you wish to prevent, for example, an email address being used as a username put in the following:

Code: Select all

if(preg_match('/@/i', $username)) return false;
Be careful however as this will not allow the '@' symbol to be used which is used in all email addresses. It could be used elsewhere and then you have an issue. There is another way to prevent an email address from being used by checking if it is a valid email address then not allowing it. (More on how to do that in another post)

What I have also done is create a list of unacceptable usernames and put them in a text file. I then have a line of code which checks to see if any of the usernames typed in are in that list and if so not allow them to be used.

Enjoy

Alan