'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');
Share:
47,008
Scott
Author by

Scott

Updated on July 05, 2022

Comments

  • Scott
    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
    None about 13 years
    should be !/\S/.test(typeof(str) == 'string' ? str : ''). If it's not, null, undefined, any any object will return true.
  • Neil
    Neil about 13 years
    I would write it as !str || !/\S/.test(str);
  • caffeine.storm
    caffeine.storm about 13 years
    g = 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
    Peter Bailey about 13 years
    @cwolves what browser do you use where HTMLInputElement.value ever returns null? Just because the question asker thinks "null" is a possible value doesn't mean it actually is.
  • None
    None about 13 years
    none, but you're writing a generic function that may be used in another context :)
  • Peter Bailey
    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
    Randy Burden about 12 years
    This technique only works for blank strings such as "" and not for strings with multiple whitespace characters.
  • Rhyous
    Rhyous over 9 years
    Does not work with undefined. plnkr.co/edit/oHy8iVk7qio12Klpoh72?p=preview
  • Rhyous
    Rhyous over 9 years
    This fails for both null and undefined values: plnkr.co/edit/zWruFu?p=preview
  • caffeine.storm
    caffeine.storm over 9 years
    Updated to support undefined. Your other test case is failing before it enter this method @Rhyous.
  • Rhyous
    Rhyous over 9 years
    Dexter, 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
    Shivanshu Goyal over 6 years
    This doesn't check for null values
  • kybernetikos
    kybernetikos over 6 years
    if textBoxVal is null, then the first part of your condition will throw. You need the textBoxVal === null first.
  • ccordon
    ccordon over 5 years
    Your solution is very good maybe you can add this if (!input) return true; in the condition to make the code simpler @Dexter
  • caffeine.storm
    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
    ccordon over 5 years
    @Dexter the code works very well with the change here is an example
  • Johannes Buchholz
    Johannes Buchholz over 5 years
    String.prototype.trim is part of JavaScript since v5.1 / June 2011
  • Kyle Delaney
    Kyle Delaney about 5 years
    Why use Regex instead of trim()?
  • caffeine.storm
    caffeine.storm about 5 years
    This 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
    Atomosk almost 3 years
    It's funny how an 'easy enough' solution unnecessarily doubles memory requirements.