1

I have to check some textField with the following regex:

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

It works with regex checkers but it doesn't work with my GWT/GXT application. I use the function above in a class that extends AbstractValidator.

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

I use GXT2 and GWT 1.6.4.

Mac Fly
  • 359
  • 1
  • 7
  • 22

4 Answers4

4

have you tried using String.macthes for validate the value and Validator class in gxt TextField?

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

OK for gxt2...

the corresponding method is setRegex(String regex) which will get called if all other validators have passed

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

Why don't you use (for gxt3):

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

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

see gxt field api

see gxt RegExValidator

user1258245
  • 3,639
  • 2
  • 18
  • 23
0

Why go native, while you can still use Java ? Or even better, try to see if GXT gives you some validation tools. Like Validators

Related : Regular Expressions and GWT

Community
  • 1
  • 1
Jean-Michel Garcia
  • 2,359
  • 2
  • 23
  • 44
0

Sencha GXT4:

enum 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 class


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;
  }
}

How to use with textField


   field.addValidator(new VTypeValidator(VType.EMAIL,"Invalid email"));