Skip to content

Instantly share code, notes, and snippets.

@EdCharbeneau
Created March 14, 2014 17:11
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save EdCharbeneau/9552248 to your computer and use it in GitHub Desktop.
Javascript test a string to see if it is Null or Whitespace. Equivelent to C# String.IsNullOrWhiteSpace
function isNullOrWhiteSpace(str) {
return (!str || str.length === 0 || /^\s*$/.test(str))
}
@jeremygithub
Copy link

Thanks!
But I'd like to attach it to String, so that I could use it like this:

if('hello'.isNullOrWhiteSpace()){
alert('Empty');
}else{
alert('Not empty');
}

@zoldello
Copy link

I think a better name for this is: "isFalseyOrWhiteSpace"

@DorkForce
Copy link

Thread resurrection here, but in case anyone stumbles upon this, I believe the answer Jeremygithub was looking for was:

String.prototype.isNullOrWhiteSpace = function() { return (!this || this.length === 0 || /^\s*$/.test(this)) }

@Yogiaccount
Copy link

if i have

@Html.EditorFor(m=>m. name) @Html.EditorFor(m=>m. lastname) how i defind that name is empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment