public static boolean validatePrice(Object bean, ValidatorAction va, Field field, Errors errors) { String value = ValidationUtil.extractValue(bean, field); String regex = "^\\d{0,10}\\.?\\d{0,2}$"; if (value != null && !Pattern.matches(regex, value)) { FieldChecks.rejectValue(errors, field, va); return false; } return true; }
/** * 简单的检查是否有非法字符。 * * @param bean * @param va * @param field * @param errors * @return */ public static boolean validateText(Object bean, ValidatorAction va, Field field, Errors errors) { String value = ValidationUtil.extractValue(bean, field); String regex = ".*\\p{Punct}.*"; if (Pattern.matches(regex, value)) { FieldChecks.rejectValue(errors, field, va); return false; } return true; }
public static boolean validatePositiveInteger( Object bean, ValidatorAction va, Field field, Errors errors) { String value = ValidationUtil.extractValue(bean, field); String regex = "^\\+?[1-9]\\d*$"; if (value != null && !Pattern.matches(regex, value)) { FieldChecks.rejectValue(errors, field, va); return false; } return true; }
/** * Checks if the field is a valid code string. A word character: [a-zA-Z_0-9] * * @param bean The bean validation is being performed on. * @param va The <code>ValidatorAction</code> that is currently being performed. * @param field The <code>Field</code> object associated with the current field being validated. * @param errors The <code>Errors</code> object to add errors to if any validation errors occur. * -param request Current request object. * @return A Date if valid, a null if blank or invalid. */ public static boolean validateCode(Object bean, ValidatorAction va, Field field, Errors errors) { String value = ValidationUtil.extractValue(bean, field); // 简单处理使其支持中划线 // value=value.replace("-","_"); String regex = "[\\w-#]*"; if (value != null && (!Pattern.matches(regex, value))) { FieldChecks.rejectValue(errors, field, va); return false; } return true; }
/** * 只是简单检查是否含有HTML标记 * * @param bean * @param va * @param field * @param errors * @return */ public static boolean validateNoHtml( Object bean, ValidatorAction va, Field field, Errors errors) { String value = ValidationUtil.extractValue(bean, field); if (value != null && (value.indexOf("<") != -1 || value.indexOf(">") != -1)) { return false; } // String regex = ".*<(\\w+)\\b[^>]*>.*?</\\1>.*|.*<(\\w+)\\b[^>]*/>.*"; // // if (Pattern.matches(regex, value)) { // FieldChecks.rejectValue(errors, field, va); // return false; // } return true; }