sap.m.Input with Float type

时间:2016-04-04 18:41:30

标签: javascript sapui5

My requirement is to have a textbox that can accept only floating point numbers up to two decimals only.

How to fix below problems:

  1. I am unable to type anything like 20.01 but I am able to type 20.10. So when you start typing 0 after decimal point, it doesn't let you do that.
  2. When I start typing 66.66 -> after this if you press 6 one more time it starts rounding off the last decimal. My requirement is not to allow the user to type anything else after last two decimal digits.

Below is my code:

sap.ui.getCore().setModel(new sap.ui.model.json.JSONModel(), "myModel");
sap.ui.getCore().getModel("myModel").setProperty("/Amount")
var textbox = new sap.m.Input({
  valueLiveUpdate: true,
  type: "Number",
}).placeAt("content");
textbox.bindProperty("value", {
  path: "myModel>/Amount",
  type: new sap.ui.model.type.Float({
    minIntegerDigits: 1,
    maxFractionDigits: 2,
    groupingEnabled: false
  }, {
    minimum: 1,
    maximum: 2000
  })
});
textbox.attachValidationError(function(e) {
  e.getParameter("element").setValueState("Error");
  e.getParameter("element").setValueStateText(e.getParameter("message"));
});
textbox.attachParseError(function(e) {
  e.getParameter("element").setValueState("Error");
  e.getParameter("element").setValueStateText(e.getParameter("message"));
});
textbox.attachFormatError(function(e) {
  e.getParameter("element").setValueState("Error");
  e.getParameter("element").setValueStateText(e.getParameter("message"));
});
textbox.attachValidationSuccess(function(e) {
  e.getParameter("element").setValueState("Success");
});
<!DOCTYPE html>
<html>

<head>
  <script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js" id="sap-ui-bootstrap" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-libs="sap.m"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>
  <div id="content" />
</body>

</html>

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我确定你已经这样做了,但是你看过documentation samples了吗? 它看起来应该做你想要的。 如果没有,您总是可以编写一个简单的函数来检查输入值与正则表达式的对比。