You can add the space character to your character class to be excluded.
^[^\n ]*$
Regular expression
^ # the beginning of the string
[^\n ]* # any character except: '\n' (newline), ' ' (0 or more times)
$ # before an optional \n, and the end of the string