煎茶GXT4:
枚舉由Vtype
public enum VType {
ALPHABET("^[a-zA-Z_]+$", "Alphabet"), ALPHANUMERIC("^[a-zA-Z0-9_]+$",
"Alphanumeric"), NUMERIC("^[+0-9]+$", "Numeric"), EMAIL("^(\\w+)([-+.][\\w]+)*@(\\w[-\\w]*\\.){1,5}([A-Za-z]){2,4}$","Email");
String regex;
String name;
VType(String regex, String name) {
this.regex = regex;
this.name = name;
}
}
Validator類
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.EditorError;
import com.sencha.gxt.widget.core.client.form.Validator;
import com.sencha.gxt.widget.core.client.form.error.DefaultEditorError;
public class VTypeValidator implements Validator<String> {
private String message;
private final VType type;
public VTypeValidator(VType type,String message){
this.type = type;
this.message=message;
}
@Override
public List<EditorError> validate(Editor<String> editor, String value) {
List<EditorError> res = null;
if (!value.matches(type.regex)) {
List<EditorError> errors = new ArrayList<EditorError>();
errors.add(new DefaultEditorError(editor, message,""));
return errors;
}
return res;
}
}
如何到u SE與文本框
field.addValidator(new VTypeValidator(VType.EMAIL,"Invalid email"));
我用gxt2,所以這個類不存在... – 2012-07-31 11:59:14