'IsNullOrWhitespace' in JavaScript?
47,008
Solution 1
It's easy enough to roll your own:
function isNullOrWhitespace( input ) {
if (typeof input === 'undefined' || input == null) return true;
return input.replace(/\s/g, '').length < 1;
}
Solution 2
no, but you could write one
function isNullOrWhitespace( str )
{
// Does the string not contain at least 1 non-whitespace character?
return !/\S/.test( str );
}
Solution 3
Try this out
Checks the string if undefined, null, not typeof string, empty or space(s
/**
* Checks the string if undefined, null, not typeof string, empty or space(s)
* @param {any} str string to be evaluated
* @returns {boolean} the evaluated result
*/
function isStringNullOrWhiteSpace(str) {
return str === undefined || str === null
|| typeof str !== 'string'
|| str.match(/^ *$/) !== null;
}
You can use it like this
isStringNullOrWhiteSpace('Your String');
Author by
Scott
Updated on July 05, 2022Comments
-
Scott almost 2 years
Is there a JavaScript equivalent to .NET's
String.IsNullOrWhitespace
so that I can check if a textbox on the client-side has any visible text in it?I'd rather do this on the client-side first than post back the textbox value and rely only on server-side validation, even though I will do that as well.
-
None about 13 yearsshould be
!/\S/.test(typeof(str) == 'string' ? str : '')
. If it's not, null, undefined, any any object will return true. -
Neil about 13 yearsI would write it as
!str || !/\S/.test(str);
-
caffeine.storm about 13 yearsg = global, i = case insensitive. Actually probably don't need the i. By default javascript replace only replaces the first match, but with the g it replaces all instances.
-
Peter Bailey about 13 years@cwolves what browser do you use where
HTMLInputElement.value
ever returnsnull
? Just because the question asker thinks "null" is a possible value doesn't mean it actually is. -
None about 13 yearsnone, but you're writing a generic function that may be used in another context :)
-
Peter Bailey about 13 years@cwolves I guess we just interpreted the "null" in the function name differently. I took it to be "null string", not the actual
null
value. en.wikipedia.org/wiki/Empty_string -
Randy Burden about 12 yearsThis technique only works for blank strings such as "" and not for strings with multiple whitespace characters.
-
Rhyous over 9 yearsDoes not work with undefined. plnkr.co/edit/oHy8iVk7qio12Klpoh72?p=preview
-
Rhyous over 9 yearsThis fails for both null and undefined values: plnkr.co/edit/zWruFu?p=preview
-
caffeine.storm over 9 yearsUpdated to support undefined. Your other test case is failing before it enter this method @Rhyous.
-
Rhyous over 9 yearsDexter, I've given up on "undeclared". Also, I found another site that shows !input works for both (typeof input === 'undefined' || input == null). It works in my tests. Thanks by the way! Sometimes in short comments mentioning a problem, we forget to mention how much the answer helped. Your answer got me started.
-
Shivanshu Goyal over 6 yearsThis doesn't check for null values
-
kybernetikos over 6 yearsif textBoxVal is null, then the first part of your condition will throw. You need the textBoxVal === null first.
-
ccordon over 5 yearsYour solution is very good maybe you can add this
if (!input) return true;
in the condition to make the code simpler @Dexter -
caffeine.storm over 5 years@CarlosEduardoCordón, this doesn't quite do the same thing as the code above -
!input
will return true for values like the string "false", as an example. Have a look here for more examples: developer.mozilla.org/en-US/docs/Glossary/Falsy -
ccordon over 5 years@Dexter the code works very well with the change here is an example
-
Johannes Buchholz over 5 yearsString.prototype.trim is part of JavaScript since v5.1 / June 2011
-
Kyle Delaney about 5 yearsWhy use Regex instead of
trim()
? -
caffeine.storm about 5 yearsThis was posted in 2011, before trim() was part of the spec - but yes, you could use trim() now! (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…)
-
Atomosk almost 3 yearsIt's funny how an 'easy enough' solution unnecessarily doubles memory requirements.