开头限制space

Restricting space at the beginning

这是我在 liveChange 事件中使用的功能:

if (oEvent.mParameters.value.indexOf(" ") === 0) {
  sap.m.MessageToast.show("Space character is not allowed");
  oEvent.preventDefault();   
  return false;
}

通过使用此代码,我必须限制输入字段开头的空格。我尝试了很多,我收到了消息,但它占用了空白。我怎样才能使输入字段只显示消息并且它不应该有空格。

要"invasively"删除输入开头的空格,你可以这样做:

onLiveChange = function(oEvent) {
  var sValue = oEvent.getParameter('value');
  if (sValue[0] === ' ') { // or use sValue.match(/^ /)
    oEvent.getSource().setValue(sValue.trimStart());
    sap.m.MessageToast.show("Space character is not allowed");
  }
}

另请参阅此 SAPUI5 Demokit Sample as well as the validationError and validationSuccess events of class `sap.ui.core.Core' 以了解推荐的验证处理方式。