2012-07-31 78 views
1

一個文本框我要檢查一些文本框與下面的正則表達式:驗證與GXT

[\sa-zA-Z.'-àáâãäåçèéêëìíîïðòóôõöùúûüýÿ]+ 

它與正則表達式的跳棋,但它並沒有與我的GWT/GXT應用程序。我在擴展AbstractValidator的類中使用上面的函數。

public static native boolean isValidName(String name) /*-{ 
    var pattern = /[\sa-zA-Z.\'-àáâãäåçèéêëìíîïðòóôõöùúûüýÿ]+/; 
    return pattern.test(name); 
}-*/; 

我使用GXT2和GWT 1.6.4。

回答

0

確定爲gxt2 ...

相應的方法是setRegex(String regex)如果所有其他驗證已通過

http://www.jarvana.com/jarvana/view/com/extjs/gxt/2.2.0/gxt-2.2.0-javadoc.jar!/com/extjs/gxt/ui/client/widget/form/TextField.html

你爲什麼不使用(gxt3),這將被調用:

com.sencha.gxt.widget.core.client.form.Field<T>.addValidator(Validator<T> validator) 

field.addValidator(new RegExValidator("[\sa-zA-Z.\'-àáâãäåçèéêëìíîïðòóôõöùúûüýÿ]", "some message")); 

看到gxt field api

請參閱gxt RegExValidator

+0

我用gxt2,所以這個類不存在... – 2012-07-31 11:59:14

0

爲什麼要本地化,而您仍然可以使用Java?或者更好,試着看看GXT是否給你一些驗證工具。像Validators

相關:Regular Expressions and GWT

+0

,因爲該項目採用GXT2和GWT 1.9.4 ... – 2012-07-31 12:12:09

+0

@MacFly你檢查我已經鏈接張貼? – 2012-07-31 12:20:25

+0

是的,但這個類在GWT 1.9.4中不存在... – 2012-07-31 12:32:26

4

你嘗試過在GXT TextField使用String.macthes用於驗證的值和Validator類?

someTextField.setValidator(new Validator() { 
     @Override 
     public String validate(Field<?> field, String value) { 
      return value.matches("[\\sa-zA-Z.'-àáâãäåçèéêëìíîïðòóôõöùúûüýÿ]+") 
           ? null : "not a valid value"; 
     } 
    }); 
0

煎茶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")); 
+0

參考:https://biancama.wordpress.com/2011/06/20/gwt-gxt-using-client-validation/ – user3698270 2016-11-28 02:23:53