sapui5中必填字段验证器 [英] Required field validator in sapui5

查看:229
本文介绍了sapui5中必填字段验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个带有标签名称,年龄,城市和电话号码的文本字段。如果它留空,我必须验证它。它应该提醒我填写。如何在sapui5中使用必填字段验证器验证文本字段?

I have four text fields with labels name, age, city and phone number.I have to validate it if it left empty. it should alert me to fill. How to validate a text field with required field validator in sapui5?

推荐答案

您可以简单地编写一个获取文本字段并检查其值的函数。

这可能看起来像这样:

You can simply write a function that get the textfields and checks their value.
This could look like this:

function validateTextFieldValues() {

    // this function gets the first textfield
    var nameTextField = sap.ui.getCore().byId("idOfTheTextFieldContaingTheName");
    //this function checks its value, you can insert more checks on the value
    if(nameTextField.getValue() === "") {
        alert("Please enter a name.");
    }

    // ...
    // the same for the other fields
}

然后你可以点击按钮来绑定该功能,例如在创建按钮时:

Then you can bind the function on the button-click, for example when creating the button:

new sap.ui.commons.Button({
    // your buttonconfiguration
    click: function(){
        validateTextFieldValues();
    }
});




此外 TextField 有一个名为 valueState 的属性。

与其事件有关 liveChange 键入时有机会验证输入:



Additionally the TextField has an attribute called valueState.
In connection with its event liveChange there is the opportunity to validate the input while typing:

new sap.ui.commons.TextField({
    // your configuration
    liveChange: function(){
        if(this.getValue() === "")
            this.setValueState(sap.ui.core.ValueState.Error);
        else
            this.setValueState(sap.ui.core.ValueState.Success);
    }
});

https://openui5.hana.ondemand.com/docs/api/symbols/sap.ui.core.ValueState.html

这篇关于sapui5中必填字段验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆