2017-01-09 34 views
0

我想在輸入字段中設置類型設置爲Number時的驗證並使用sapui5設置maxlength。但它不起作用。從api sap.m.Input的方法setMaxLength的如果輸入類型設置爲Number如何使用sapui5設置maxlength

var Price = new sap.m.HBox({ 
    items:[new sap.m.Label({text:"Per Unit Price",required:true,design:"Bold",width:"110px"}), 
    new sap.m.Input("Price",{ width:"150px",type:"Number", 
      value:{ 
       type : 'sap.ui.model.type.Integer', 
       constraints : { 
       minLength: 1, 
       maxLength: 15 
      } 
     } 

    })] 
}); 

回答

1

描述:

此參數不與輸入類型sap.m.InputType.Number兼容。如果輸入類型設置爲Number,則maxLength值將被忽略。

所以這意味着你必須找到其他方式來做到這一點。例如,當您使用sap.m.InputType.Tel格式代替,最大長度方法的工作原理:

var oInput = new sap.m.Input("Price",{ 
     width:"150px", 
     type:"Tel", 
     minLength: 1, 
     maxLength: 15 
});   
oInput.placeAt('content'); 

Here是互動的例子。

EDITED 17:30 090117:

我以前編輯代碼發佈允許輸入你想要的(請儘量here)只是數字:

var sNumber = ""; 
    var oInput = new sap.m.Input("Price",{ 
      width:"150px",     
      minLength: 1, 
      maxLength: 15, 
      liveChange : function(oEvent){      
          var value = oEvent.getSource().getValue(); 
          var bNotnumber = isNaN(value); 
          if(bNotnumber == false)sNumber = value; 
          else oEvent.getSource().setValue(sNumber);      
      }, 

    });   
    oInput.placeAt('content'); 
+0

謝謝Jaro ...它工作。 –

+0

不客氣。 – Jaro

0

正如哈羅指出,阿比說:如果輸入類型設置爲Number,則maxLength值將被忽略。因此,刪除輸入字段的類型,並保持它的默認字符串,並設置最小和最大長度:

var oInput = new sap.m.Input({ 
      value:{ 
        type : 'sap.ui.model.type.String', 
        path:"/value", 
        constraints : { 
        maxLength:2, 
        minLength:1 
       } 
      } 
     }); 

以上代碼不允許我進入超過99(2位)。

編輯:如果需要數字類型輸入字段。 下面的代碼不會讓我成功輸入超過100的值。

var oInput = new sap.m.Input({ 
      type:'Number', 
      value:{ 
        type : 'sap.ui.model.type.Integer', 
        path:"/value", 
        constraints : { 
        minimun:0, 
        maximum:100 
       } 
      } 
     }); 
+0

感謝#Rahul作爲答案,但我希望輸入類型爲數字,然後限制輸入字段的最大長度。 –

+0

@PriyankaMaurya爲數字類型輸入字段更新了答案。 –

相關問題