properly detect invalid characters