All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sksamuel.jqm4gwt.form.validators.RegexValidator Maven / Gradle / Ivy

The newest version!
package com.sksamuel.jqm4gwt.form.validators;

import com.google.gwt.user.client.ui.HasValue;

/**
 * @author Stephen K Samuel [email protected] 13 Jul 2011 00:45:29
 * 
 *         An implementation of {@link Validator} that tests that the value from
 *         a value producing {@link HasValue} instance matches a supplied regex.
 * 
 */
public class RegexValidator implements Validator {

	private final HasValue	hasValue;
	private final String			regex;
	private final String			msg;

	public RegexValidator(HasValue hasValue, String regex) {
		this(hasValue, regex, "This field is not in the correct format");
	}

	public RegexValidator(HasValue hasValue, String regex, String msg) {
		this.hasValue = hasValue;
		this.regex = regex;
		this.msg = msg;
	}

	@Override
	public String validate() {
		String value = hasValue.getValue();
		if (value == null || value.trim().length() == 0)
			return null;

		if (value.matches(regex))
			return null;

		return msg;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy