There appears to be settings in site.ini that will allow you to do this without any coding. See settings/site.ini under [UserSettings]
# Custom validation of user login name
# Will only affect new users, and existing users when they edit their credentials
UserNameValidationRegex[]
UserNameValidationErrorText[]
DefaultUserNameValidationErrorText=User login name did not validate!
# Examples of validation
#UserNameValidationRegex[sw]=/^\s/
#UserNameValidationErrorText[sw]=The username cannot start with a whitespace.
#UserNameValidationRegex[ew]=/\s$/
#UserNameValidationErrorText[ew]=The username cannot end with a whitespace.
#UserNameValidationRegex[rw]=/\s+/
#UserNameValidationErrorText[rw]=The username cannot contain repeated whitespace.
#UserNameValidationRegex[lb]=/\n|\r/
#UserNameValidationErrorText[lb]=The username cannot contain line breaks.
#UserNameValidationRegex[t]=/\t/
#UserNameValidationErrorText[t]=The username cannot contain tabs.
# Alternative example only allowing 'word' charthers
#UserNameValidationRegex[w]=/\W/
#UserNameValidationErrorText[w]=The username can only contain a-z, digits or underline.
# Or
#UserNameValidationRegex[wh]=/[^A-Za-z0-9_-]/
#UserNameValidationErrorText[wh]=The username can only contain a-z, digits, underline or hyphen.
# While testing add this at the end, just remember to remove it when your done..
#UserNameValidationRegex[test]=/\w/
#UserNameValidationErrorText[test]=The username seems to be ok!
# Custom validation of user login name
# Will only affect new users, and existing users when they edit their credentials
Out of curiosity, how is that possible or make sense for existing users if login name cannot change? Or can it? If your login doesn't pass validation because the validation rules had changed and then changing login is also not permitted... what am I missing here? ;)
As expected, new validation rules make it impossible to (re)publish the user object if the login contains characters out of range. Created an issue for it: http://issues.ez.no/IssueView.php?Id=15621