首页 > 编程 > Java > 正文

javaBean字段验证

2019-11-08 20:15:35
字体:
来源:转载
供稿:网友
package com.wewb.web.util;import org.apache.commons.lang.StringUtils;  /**      * 版权所有:2017-TTKD   * 项目名称:ttkd_front      *   * 类描述:   * 类名称:com.wewb.web.util.ValidateBeanUtil        * 创建人:IMF_WOLF    * 创建时间:2017年2月15日 下午4:49:57      * 修改人:   * 修改时间:2017年2月15日 下午4:49:57      * 修改备注:      * @version   V1.0       */    public class ValidateBeanUtil {	public static int STRING = 1;	public static int PHONGORTEL = 2;	public static int PHONE = 3;	public static int TEL = 4;	public static int EMAIL= 5;	public static int POSITIVE_INTEGER = 6;	public static int DOBULE = 7;		/**	 * 类字段校验方法	 * @Title: validateColums  	 * @Description: TODO(类字段校验方法)	 * @param columsValue  	 * @param type	 * @param isNull 是否可以为空null,空字符,制表符,可以:true;不可以:false 	 * @param max	 * @param min	 * @return	 */	public static String validateColums(String columsValue, int type, boolean isNull,			int max, int min, String remarks) {		String result = null;		if (!isNull && StringUtils.isBlank(columsValue)) {			return remarks + "不能为空";		}		switch (type) {			case 1:				if (!StringUtils.isBlank(columsValue)) {					if (columsValue.trim().length() < min							|| columsValue.trim().length() > max)						result = remarks + "必须在" + min + "位至" + max + "位之间";				}				break;			case 2:				if (!StringUtils.isBlank(columsValue)) {					if (!(RegexUtils.checkMobile(columsValue) || RegexUtils							.checkPhone(columsValue)))						result = remarks + "必须为手机号码或者固定电话";				}				break;			case 3:				if (!StringUtils.isBlank(columsValue)) {					if (!(RegexUtils.checkMobile(columsValue)))						result = remarks + "必须为手机号码";				}				break;			case 4:				if (!StringUtils.isBlank(columsValue)) {					if (!(RegexUtils.checkPhone(columsValue)))						result = remarks + "必须为固定电话";				}				break;			case 5:				if (!StringUtils.isBlank(columsValue)) {					if (!RegexUtils.checkEmail(columsValue))						result = remarks + "必须符合Email格式";				}				break;			case 6:				if (!StringUtils.isBlank(columsValue)) {					if (!RegexUtils.checkDigit(columsValue))						result = remarks + "必须为整数";					else if(columsValue.length() > 12)						result = remarks + "值超出范围";					else if(Long.parseLong(columsValue) < min || Long.parseLong(columsValue) > max)						result = remarks + "必须在" + min + "至" + max + "之间";				}				break;			case 7:				if (!StringUtils.isBlank(columsValue)) {					if (!RegexUtils.checkDecimals(columsValue))						result = remarks + "必须为数值";					else if(columsValue.length() > 12)						result = remarks + "值超出范围";					else if(Double.parseDouble(columsValue) < min || Double.parseDouble(columsValue) > max)						result = remarks + "必须在" + min + "至" + max + "之间";				}				break;			}		return result;	}	public static void main(String[] args) {		System.out.PRintln(validateColums("29894.51121122", 7, false, 20, 2, "姓名"));	}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表