com.sksamuel.jqm4gwt.form.validators.RegexValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jqm4gwt-standalone Show documentation
Show all versions of jqm4gwt-standalone Show documentation
jqm4gwt bundled with all of its dependencies
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;
}
}