如何验证 SmartGWT 中的纯数字文本框?

How to validate number only textbox in SmartGWT?

文本框应该只接受数字,我想使用 ChangedHandler/ Changehandler/ KeyPressHandler

以外的任何其他处理程序

我的验证class,

public class UnderLyingIDChangeHandler implements ChangedHandler {
    private final CreditRiskView creditRiskView;

    public UnderLyingIDChangeHandler(CreditRiskView creditRiskView) {
        this.creditRiskView = creditRiskView;
    }
    @Override
    public void onChanged(ChangedEvent event) {
        String value= (String) event.getItem().getValue();
        if(!value.matches("[0-9]*")){
            creditRiskView.invalidUnderlyingID();
        }
}

这是我需要显示验证的主要class

public class CreditRiskView{
private TextItem underlyingIDField;
public void addunderlyingIDInputChangeHadler(ChangedHandler changedHandler) {

    //logic is that this method will invoked in the UnderLyingIDChangeHandler class

     underlyingIDField.addChangedHandler(changedHandler);
    }
 public void invalidUnderlyingID(){

    // I don't know how to set an error message as underlyingIDField.clearValue() 
       method is not doing well.

    }   
}

限制用户可以输入的字符怎么样? 请看这个样本: http://www.smartclient.com/smartgwt/showcase/#form_keypress_filter

如果文本框是 DynamicForm 中的 TextItem,它的工作方式如下:

IsIntegerValidator isIntegerValidator = new IsIntegerValidator();
isIntegerValidator.setErrorMessage("error message");
textItem.setValidators(isIntegerValidator);

要在调用 form.validate() 时显示错误,您需要设置 setShowInlineErrors(true) 在表格中。