How to check username policy before calling core_users_create_user

How to check username policy before calling core_users_create_user

by Jeff Shrek -
Number of replies: 2

When creating a new user with core_users_create_user, how do I check the username policy that is defined in Moodle security config to make sure the username will comply?

Thanks


Average of ratings: -
In reply to Jeff Shrek

Re: How to check username policy before calling core_users_create_user

by Jeff Shrek -

To answer my own question, I figured out that there is no username policy specified in config (only password policy).

The default behavior for Moodle username is: Moodle username policy only allows alphanumeric lowercase characters (letters/numbers) and underscore ( _ ) and hyphen( - ) and period( . ) and at-symbol( @ )

So using preg_replace you can eliminate everything that should not be there with this:

$username = strtolower($username); //convert any UPPERCASE to lowercase
$username = preg_replace('/[^-.@_0-9a-z]/', '', $username); //remove all other characters that are not allowed
Average of ratings: Useful (1)
In reply to Jeff Shrek

Re: How to check username policy before calling core_users_create_user

by Albert Leatherman -

Hi Jeff, do you know if there is a way to display the username policy on the signup page, just as the password policy is displayed (see attached pic)? Thanks.