---
title: "RegexValidator "
author: "rainliu"
date: "2012-09-05T13:50:43Z"
tags: []
categories: ["编程语言"]
slug: "regexvalidator"
draft: false
lastmod: "2012-09-04T13:45:38Z"
id: 1671124
url: "https://rainliu.iteye.com/blog/1671124"
---

```
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexValidator extends BaseValidator implements GracieValidator {
    
    public RegexValidator() {
        
    }
    
    /**
     * Use regular expression for matching strings of input text
     * @param inputText          Input string text
     * @return boolean           Return true when it is matching for the regular expression or the input text is empty,
     *                           otherwise return false.
     */    
    public static boolean isValid(String regex, String inputText) {
        boolean isValid = false;
        
        if (!Util.isEmpty(inputText)) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputText);
            isValid = matcher.matches();
        } else {
            isValid = true;
        }
        
        return isValid;
    }
    
    /**
     * Use regular expression for matching strings of input text
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param fieldErrorMessage  Field error message
     * @return boolean           Return true when it is matching for the regular expression or the input text is empty,
     *                           otherwise return false.
     */    
    public static boolean isValid(String regex, String inputText, String fieldErrorParam, String fieldErrorMessage) {
        boolean isValid = RegexValidator.isValid(regex, inputText);
        
        if (!isValid) {
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    /**
     * Use regular expression for matching strings of input text
     * @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 it is matching for the regular expression or the input text is empty,
     *                           otherwise return false.
     */    
    public static boolean isValid(String regex, String inputText, String fieldErrorParam, 
            String bundle, String key, String defaultMessage, String[] arguments) {
        boolean isValid = RegexValidator.isValid(regex, inputText);
        
        if (!isValid) {
            String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }

	public boolean isValid(String inputText) {
		return false;
	}
    
	public boolean isValid(String inputText, Object[] objs) {
		String regex = (String) objs[0];
		return RegexValidator.isValid(regex, inputText);
	}

	public boolean isValid(String inputText, String fieldErrorParam, String bundle, String key, String defaultMessage, String[] arguments) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isValid(String inputText, String fieldErrorParam, String fieldErrorMessage) {
		// TODO Auto-generated method stub
		return false;
	}
	
	

}
```