A generic function in JavaScript similar to"String.IsNullOrEmpty" of C#, to check whether the specified string is null or empty.
How often we check a string for Null, Undefined & empty string in a JavaScript code. Here is my very basic method
StringHasValue to check if a string is either null, undefined or if it’s an empty string (String to be checked may contain white-spaces, that is also considered). Although it’s a very basic method but definitely it is going to save our time, less coding efforts & it will be less error prone.
- var StringHasValue = function(strValue){
- if($.trim(strValue) != "" && $.trim(strValue) != null && $.trim(strValue) != undefined)
- return true;
- else
- return false;
- };
This function return boolean, true if string has some value & false if its null or an empty string.
We can simply use this function in our entire appliication like this:
- if(StringHasValue($("#ddlCountries").val())
- alert("Yes");
- else
- alert("No");