4、校验工具
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
import java.util.ArrayList; import java.util.List;import org.springframework.util.Assert;/*** 校驗(yàn)工具類(lèi)**/ public abstract class ValidateUtil {/** 用戶(hù)名正則 */private static final String REG_USERNAME = "^[a-zA-Z]\\w{3,31}";/** Email 正則 */private static final String REG_EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";/*** 驗(yàn)證必填項(xiàng),如果不通過(guò),返回List【錯(cuò)誤描述信息】* * @param dataObj 校驗(yàn)對(duì)象* @param propNames 校驗(yàn)屬性【數(shù)組】* @param displayNames 屬性對(duì)應(yīng)的【中文名稱(chēng)】* @return errorList 錯(cuò)誤集合,每條數(shù)據(jù)表示一個(gè)屬性的錯(cuò)誤描述信息*/public static List<String> validateMust(Object dataObj, String[] propNames,String[] displayNames) {Assert.isTrue(propNames.length == displayNames.length, "參數(shù)名與顯示名的個(gè)數(shù)不一致");List<String> errorList = new ArrayList<String>();for (int i = 0; i < propNames.length; i++) {String propName = propNames[i];String displayName = displayNames[i];Object value = ReflectUtil.getProperty(dataObj, propName);if (null == value) {errorList.add(displayName + "(" + propName + ")是必填項(xiàng)");} else {if (value instanceof String) {// 處理全角空格String valueStr = value.toString().replace(" ", "").trim();if (valueStr.length() == 0) {errorList.add(displayName + "(" + propName + ")是必填項(xiàng)");}}}}return errorList;}/*** * 校驗(yàn)郵箱* * @param email* @return boolean*/public static boolean validateEmail(String email) {if (null == email || email.trim().length() == 0) {return false;}return email.matches(REG_EMAIL);}/*** * 校驗(yàn)用戶(hù)名* * @param* @return boolean*/public static boolean validateUserName(String userName) {return validateByRegex(userName, REG_USERNAME);}/*** 按指定正則表達(dá)式驗(yàn)證* @param input 輸入字符* @param regex 正則* @return 成立與否*/private static boolean validateByRegex(String input, String regex) {if (null == input || input.trim().length() == 0) {return false;}return input.matches(regex);}}?
轉(zhuǎn)載于:https://my.oschina.net/u/3551274/blog/1796790
總結(jié)
- 上一篇: java1.8--改进的接口
- 下一篇: Python的gevent协程及协程概念