How can i prevent usernames from containing special characters?

Discussion in 'Customization & add-ons' started by alexblyz91, Oct 23, 2019.

  1. alexblyz91

    alexblyz91 New Member

    Joined:
    Aug 9, 2019
    Messages:
    24
    I want to prevent users from registering using usernames that contain special characters such as '-'.
    How can i prevent this?
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Hello,

    Here is code that allow to use only numbers and latters within username.
    You need to put this code snippet to site.php file:
    http://www.amember.com/docs/Site.php_file
    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::GET_LOGIN_REGEX, function (Am_Event $e) {
        
    $e->setReturn('/^[0-9a-zA-Z]+$/');
    });
    Also you need to change error message 'Username contains invalid characters - please use digits, letters, dash and underscore' accordingly. You can do it either at:
    aMember CP -> Utilities -> Edit Messages

    edit-messages.png

    or within form configuration (click labels on username block)
    aMember CP -> Configuration -> Forms Editor

    username-error.png

    Best Regards.

Share This Page