What is the best way to check if a string contains only whitespace?
The string is allowed to contain characters combined with whitespace, but not just whitespace.
What is the best way to check if a string contains only whitespace?
The string is allowed to contain characters combined with whitespace, but not just whitespace.
Simplest answer if your browser supports the
trim()
functionThe regular expression I ended up using for when I want to allow spaces in the middle of my string, but not at the beginning or end was this:
or
So, I know this is an old question, but you could do something like:
or you can do what nickf said and use:
Well, if you are using jQuery, it's simpler.
this checks for 1 or more whitespace characters, if you it to also match an empty string then replace
+
with*
.Just check the string against this regex: