Hi Everyone,
In this article I'm going to explain How to Disable the backspace and delete keys using event handler function Multi Input in SAP Ui5
Refer this link to know more about MultiInput :
https://sapui5.hana.ondemand.com/sdk/#/api/sap.m.MultiInput%23overview
Firstly In Xml File, we need to add multi input field like below with the property "token update" ,
In UI5, the MultiInput control allows users to input multiple values in a single input field. The tokenUpdate event is triggered when a token is added, removed, or changed in the MultiInput control. The
tokenUpdate event handler receives a single parameter, which is an object that contains information about the event.
To prevent users from deleting or backspacing values in a MultiInput control in SAP UI5, you can add an event handler function that disables the backspace and delete keys. This function can be triggered when a user presses a key on their keyboard while interacting with the MultiInput control.
<MultiInput id="Input1" customData:hasValue="false" valueHelpOnly="true" valueHelpRequest="onNumber" tokenUpdate="ChangeToken"></MultiInput>
This below code is used to disable the backspace and delete keys in an event handler function in JavaScript. The event handler function is triggered when a user presses a key on their keyboard.
The event.which, event.keyCode, and event.charCode properties are used to determine which key was pressed. Different browsers may use different properties, so all three are checked to ensure maximum compatibility.
The key variable is then assigned the value of the key code that was pressed. The values 8 and 46 correspond to the backspace and delete keys, respectively.
The if statement checks if the key variable matches either of these values. If it does, the disable statement is executed. However, this code on its own doesn't seem to be complete as disable is not a valid JavaScript statement, and it's unclear what exactly is being disabled.
It's possible that this code is intended to prevent the user from deleting or backspacing a value in an input field.
ChangeToken: function (oEvent) {
var key = event.which || event.keyCode || event.charCode;
if (key == 8 || key == 46)
{
disable; //or write event.preventDefault(); // prevent default behavior of backspace& delete keys
}
}
Output:
In Above picture we can see the multi input with token which can be removed by clicking on X button and tokens will not remove by clicking on delete or backspace key.
Conclusion:
In conclusion, disabling the backspace and delete keys in an event handler function for a MultiInput control in SAP UI5 can prevent users from accidentally or intentionally deleting values. By using the which, keyCode, and charCode properties of the event object to detect when the backspace or delete key is pressed, you can then use the preventDefault() method to prevent the default behavior of these keys from being executed. This can help ensure that the values in the MultiInput control remain intact and accurate, improving the overall user experience.
Happy learning folks!!
Thanks and regards,
Pooja D S