Skip to content

Instantly share code, notes, and snippets.

@ruslansavenok
Created October 7, 2013 10:50
Show Gist options
  • Save ruslansavenok/6865951 to your computer and use it in GitHub Desktop.
Save ruslansavenok/6865951 to your computer and use it in GitHub Desktop.
Checks If String Contains Only White Spaces
/*
* Checks If String Contains Only White Spaces
*
* Useful for Fields Validation $input.val().isEmpty()
*/
String.prototype.isEmpty = function () {
return /^\s*$/.test(this);
}
@CezaryDanielNowak
Copy link

Creating regexp for each function call isn't super efficient. This would be faster:

String.prototype.isEmpty = (function(){
    var r = /^\s*$/;
    return function () {
        return r.test(this);
    }
})();

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