Refactor this code to a independent logic function to check for validity of a name. This function shall not worried anything about UI. It shall return only two values which is whether the name is valid and error message.