`

AlphanumericValidator

阅读更多
public class AlphanumericValidator extends BaseValidator {
    
    private AlphanumericValidator() {
        
    }
    
    public static final String REGEX_ALPHANUMERIC = "^[A-Za-z0-9 ]+$";
    
    /**
     * Validation Rule: Only to accept "A-Z, a-z, 0-9 and space"
     * @param inputText          Input string text
     * @return boolean           Return true when input string is only composed by "A-Z, a-z, 0-9 and space" characters,
     *                           otherwise return false.
     */
    public static boolean isAlphanumeric(String inputText) {
        return RegexValidator.isValid(AlphanumericValidator.REGEX_ALPHANUMERIC, inputText);
    }
    
    /**
     * Validation Rule: Only to accept "A-Z, a-z, 0-9 and space"
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param fieldErrorMessage  Field error message
     * @return boolean           Return true when input string is only composed by "A-Z, a-z, 0-9 and space" characters,
     *                           otherwise return false.
     */
    public static boolean isAlphanumeric(String inputText, String fieldErrorParam, String fieldErrorMessage) {
        boolean isValid = AlphanumericValidator.isAlphanumeric(inputText);
        
        if (!isValid) {
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: Only to accept "A-Z, a-z, 0-9 and space"
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param bundle             Language property file id
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          The arguments of message
     * @return boolean           Return true when input string is only composed by "A-Z, a-z, 0-9 and space" characters,
     *                           otherwise return false.
     */
    public static boolean isAlphanumeric(String inputText, String fieldErrorParam, 
            String bundle, String key, String defaultMessage, String[] arguments) {
        boolean isValid = AlphanumericValidator.isAlphanumeric(inputText);
        
        if (!isValid) {
            String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics