I finally decided to swap from allowing username spaces using the following regular expression code as mentioned by Jason Rey:
^[a-zA-Z0-9]+$
While it seems that the validation works, adding a space in the username does not give a valid reason for why the username is failing as seen here:
It says "Username must be at least 4 characters long". As you can see the username is more than 4 characters long. It's not just an issue with username spaces, special characters create the same result as seen here:
If it is too difficult for the system to detect invalid characters, the default message could say "Username must be at least 4 characters long or has invalid characters".