java与js非空校验的使用
java
import org.springframework.util.StringUtils;
public class StringUtil {
/**
* 判断字符串是否为空
*/
public static boolean isEmpty(String str) {
if(str != null) {
str.trim();
}
return StringUtils.isEmpty(str);
}
/**
* 判断字符串是否非空
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* 判断是否为空
*
* @param str
* @return boolean
*/
public static boolean isNotEmpty(Object obj) {
if (obj instanceof String) {
if (null != obj && !"".equals(obj))
return true;
}
if (obj instanceof Date) {
if (null != obj)
return true;
}
if (obj instanceof Integer) {
if (null != obj)
return true;
}
if (obj instanceof Double) {
if (null != obj)
return true;
}
return false;
}
}
js
(function (window, $, undefined) {
// 定义 字符串对象(String) 扩展对象基元
coreString = function () { return String.apply(this, arguments); },
// 定义 通用工具方法 扩展对象基元
coreUtil = function () { return Object.apply(this, arguments); },
// 定义 jQuery 扩展对象基元
coreJquery = function () { return $.apply(this, arguments); },
coreString.fn = coreString.prototype = {};
coreUtil.fn = coreUtil.prototype = {};
coreJquery.fn = coreJquery.prototype = {};
coreJquery.string = coreString;
coreJquery.util = coreUtil;
// 测试对象是否是空对象(不包含任何属性)。
// 这个方法既检测对象本身的属性,也检测从原型继承的属性(因此没有使用hasOwnProperty)。
coreUtil.isEmptyObject = $.isEmptyObject;
// 测试对象是否为空(不包含任何属性的空对象、null、undefined、空字符串、全空格)。
// 这个方法既检测对象本身的属性,也检测从原型继承的属性(因此没有使用hasOwnProperty)。
coreUtil.isEmptyObjectOrNull = function (obj) {
switch (coreUtil.type(obj)) {
case "string":
return coreString.isNullOrWhiteSpace(obj);
case "array":
return obj.length == 0;
case "date":
return Date.parse(obj) == 0;
case "object":
return coreUtil.isEmptyObject(obj);
}
return obj == null || obj == undefined;
};
// 测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的)。
coreUtil.isPlainObject = $.isPlainObject;
// 判断对象是否为 "未定义" 值(即 undefined)。
coreUtil.isUndefined = function (obj) { return obj === undefined || typeof obj === "undefined"; };
// 判断对象是否为空(Null)值。
coreUtil.isNull = function (obj) { return obj === null; };
// 判断对象是否为 "未定义" 值(即 undefined)或空(Null)值。
coreUtil.isNullOrUndefined = function (obj) { return coreUtil.isUndefined(obj) || coreUtil.isNull(obj); };
// 测试对象不为 "未定义" 值(即 undefined)、空(Null)值、Boolean-False值、空字符串值或数字0中的任何一种。
coreUtil.isPositive = function (obj) { return obj ? true : false; };
// 判断对象是否为 "未定义" 值(即 undefined)、空(Null)值、Boolean-False值、空字符串值或数字0中的一种。
coreUtil.isNegative = function (obj) { return obj ? false : true; };
// 判断对象是否是一个空的 jQuery 对象(不包含任何 DOM 元素,即 length = 0)。
coreUtil.isEmptyJquery = coreUtil.isEmptyJqueryObject = function (obj) { return coreUtil.isJqueryObject(obj) && !obj.length; };
// 判断传入的对象是否是一个字符串。
coreString.isString = coreUtil.isString;
// 判断传入的字符串是否为Null或者为空字符串。
coreString.isNullOrEmpty = function (str) { return str === undefined || str === null || str === ""; };
coreString.prototype.isNullOrEmpty = function () { return coreString.isNullOrEmpty(this); };
// 判断传入的字符串是否为Null或者为空字符串或者全是空格。
coreString.isNullOrWhiteSpace = function (str) { return coreString.isNullOrEmpty(str) || coreString.trim(String(str)) === ""; };
coreString.prototype.isNullOrWhiteSpace = function () { return coreString.isNullOrWhiteSpace(this); };
})(window, jQuery);