如何在SmartGWT中验证仅限数字的文本框?

时间:2016-03-29 06:57:30

标签: validation handler smartgwt

Textbox只接受数字,我想使用ChangedHandler/ Changehandler/ KeyPressHandler以外的任何其他处理程序

我的验证课程,

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

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

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.

    }   
}

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

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