简体   繁体   English

如何在UI5中对输入使用约束属性?

[英]How to use constrains property for Input in UI5?

I'm trying to restrict the user to enter only numbers. 我试图限制用户仅输入数字。 Maybe I can do this using regular expression but I don't know the exact syntax. 也许我可以使用正则表达式来做到这一点,但我不知道确切的语法。 I don't want decimal or special characters. 我不要小数或特殊字符。

<Input type="Number"
value="{
    type : 'sap.ui.model.type.Number',
    constraints : {
        minLength: 1,
        maxLength: 15,
        validate: ...
    }
}" />

The sap.ui.model.type.Number doesn't exist, but Integer and Float do. sap.ui.model.type.Number不存在,但IntegerFloat存在。

<Input 
    type="Number"
    value="{
        path: '/number',
        type: 'sap.ui.model.type.Integer',
        formatOptions: {
            groupingEnabled: false,
            groupingSeparator: ',',
            decimalSeparator: '.'
        },
        constraints : {
            minimum: '0',
            maximum: '99'
        }
}" />

I think the integer type is just what you need. 我认为整数类型正是您所需要的。 Only integer numbers are valid and by default there is no grouping, decimals or other characters. 只有整数是有效的,默认情况下没有分组,小数或其他字符。 You could use SAPUI5's error handling capabilities to alert the user if an invalid entry has been entered. 您可以使用SAPUI5的错误处理功能来警告用户是否输入了无效的条目。

If you want to prevent invalid characters to be even entered, you could use the masked input control. 如果要防止输入无效字符,则可以使用屏蔽的输入控件。 Eg: 例如:

<MaskInput 
    mask = "999999" 
    placeholderSymbol = "_" 
    placeholder = "Enter a six digit number"/>

However, personally I find them a bit ugly for regular number. 但是,就我个人而言,我发现常规数字有点难看。 The mask input control is actually meant for input values that follow a certain pattern such as credit card number or postal codes. 掩码输入控件实际上是指遵循某些模式的输入值,例如信用卡号或邮政编码。

The very <Input type="Number" accepts nothing but numbers however if you still want to implement your own validation you can do as follow the below regex used to consider only numbers: <Input type="Number"接受数字,但是如果您仍要实现自己的验证,则可以按照以下仅考虑数字的正则表达式进行操作:

Define regex : 定义regex

regex = /^[0-9]*$/;

Add liveChange to your input liveChange添加到您的输入

    liveChange: function(oEvent){
        if(oEvent.getParameter("liveValue") === "" 
                  || !oEvent.getParameter("liveValue").match(regex)){
                this.setValueState(sap.ui.core.ValueState.Error);
   }
    else{
               this.setValueState(sap.ui.core.ValueState.Success);
    }
   }

If you restrict the input type to Number , no (special) characters are allowed to enter into the input field. 如果将输入类型限制为Number ,则不允许(特殊)字符输入输入字段。 However, number related characters ( .,- ) are allowed. 但是,允许使用数字相关的字符( .,- )。 To get rid of it, you can set the data type of your property binding to sap.ui.model.type.Integer . 要摆脱它,可以将属性绑定的数据类型设置为sap.ui.model.type.Integer You can also set the minLength , maxLength etc. constraints. 您还可以设置minLengthmaxLength等约束。 If user types in 2.1 , a validation error will be thrown and the input field value state will be set to Error. 如果用户输入2.1 ,则将引发验证错误,并且输入字段值状态将设置为Error。

<Input type="Number"
   value="{
      type : 'sap.ui.model.type.Integer',
      constraints : {
          minLength: 1,
          maxLength: 15
   }
}" />

Documentation of the Integer type. 整数类型的文档。

Here is an other example for field validation with MessageManager 这是使用MessageManager进行字段验证的另一个示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM