Example usage for org.apache.commons.validator GenericValidator matchRegexp

List of usage examples for org.apache.commons.validator GenericValidator matchRegexp

Introduction

In this page you can find the example usage for org.apache.commons.validator GenericValidator matchRegexp.

Prototype

public static boolean matchRegexp(String value, String regexp) 

Source Link

Document

Checks if the value matches the regular expression.

Usage

From source file:org.springmodules.commons.validator.FieldChecks.java

/**
 * Checks if the field matches the regular expression in the field's mask
 * attribute.// w  ww. j ava  2  s  .co m
 *
 * @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 true if field matches mask, false otherwise.
 */
public static boolean validateMask(Object bean, ValidatorAction va, Field field, Errors errors) {
    String mask = field.getVarValue("mask");
    String value = extractValue(bean, field);
    try {
        if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.matchRegexp(value, mask)) {
            rejectValue(errors, field, va);
            return false;
        } else {
            return true;
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return true;
}

From source file:org.springmodules.validation.commons.FieldChecks.java

/**
 * Checks if the field matches the regular expression in the field's mask
 * attribute.//from   ww  w  . ja  va 2 s.  c  o  m
 *
 * @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 true if field matches mask, false otherwise.
 */
public static boolean validateMask(Object bean, ValidatorAction va, Field field, Errors errors) {
    String mask = field.getVarValue("mask");
    String value = FieldChecks.extractValue(bean, field);
    try {
        if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.matchRegexp(value, mask)) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        } else {
            return true;
        }
    } catch (Exception e) {
        FieldChecks.log.error(e.getMessage(), e);
    }
    return true;
}