'IsNullOrWhitespace' in JavaScript?

Scott picture Scott · Apr 6, 2011 · Viewed 42.9k times · Source

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.

Answer

Dexter picture Dexter · Apr 6, 2011

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;
}