Home > JavaScript, jQuery > jQuery: IsNullOrEmpty plugin for jQuery

jQuery: IsNullOrEmpty plugin for jQuery

November 20th, 2012 Leave a comment Go to comments

    I was a bit disappointed when couldn’t find in jQuery library a method similar to String.IsNullOrEmpty from C#. So, below is a simple jQuery plugin to examine whether the passed string is empty or null.

(function ($) {
    $.isNullOrEmpty = function (str) {
        return !str || $.trim(str) === ""; // the trim method is provided by jQuery 
    };
})(jQuery);

Some samples of use

var res = $.isNullOrEmpty(''); // true
res = $.isNullOrEmpty("bla-bla-bla"); // false
res = $.isNullOrEmpty(null); // true
res = $.isNullOrEmpty(); // true
Related posts:
 
Categories: JavaScript, jQuery Tags: ,
  1. No comments yet.
  1. No trackbacks yet.