标签:
package com.lwj.util; import java.util.regex.Pattern; public class SetParameterUtil { /** * 验证是否为数字 * @param value * @return */ public static boolean isInteger(String value){ return Pattern.compile("^-?\\d+$").matcher(value).find(); } /** * 验证最大值最小值 * @param value * @param minValue * @param maxValue * @return */ public static boolean validateMinAndMax(String value,Integer minValue,Integer maxValue){ if(isNumber(value)){ int val = Integer.parseInt(value); if(minValue != null){ if(val < minValue){ return false; } } if(maxValue != null){ if(val > maxValue){ return false; } } }else{ return false; } return true; } public static boolean validateEmun(String value,String regex){ return Pattern.compile(regex).matcher(value).find(); } }
测试:
package com.lwj.util; public class SetParameterUtilTest { public static void main(String[] args) { boolean f = SetParameterUtil.isNumber("-1003"); System.out.println(f); String val = "63"; Integer minValue = null; Integer maxValue = 63; boolean f2 = SetParameterUtil.validateMinAndMax(val, minValue, maxValue); System.out.println(f2);
String regex = "^n10$|^n20$|^n30$|^n40$";
String value = "n40";
String value2 = "n40r";
boolean f3 = SetParameterUtil.validateEmun(value, regex);
boolean f4 = SetParameterUtil.validateEmun(value2, regex);
System.out.println(f3);
System.out.println(f4);
}
}
标签:
原文地址:http://www.cnblogs.com/qq931399960/p/5448025.html