My problem is: how to limit the length of characters from virtual keyboard. I found a virtual keyboard on codepen https://codepen.io/dcode-software/pen/KYYKxP and I would like to restrict input characters to input field maxLength. Can someone help? This is my approach, would like to last character not changing after input.
keyElement.addEventListener("click", () => {
var element = document.querySelectorAll(".use-keyboard-input");
element.maxLength = 10;
if (this.properties.value.length === element.maxLength) {
this.properties.value = this.properties.value.substring(0, this.properties.value.length - 1);
this._triggerEvent("oninput");
}
});
Unfortunately that's the wrong approach to this issue. First lets look at the problems that your approach will cause:
keyElement.addEventListener("click", () => {
if(this.properties.value.length === element.maxLength){
>=
instead of ===
.this.properties.value = this.properties.value.substring(0, this.properties.value.length-1 );
return false;
Now lets look on how to get this to work properly:
First you'll want to make the maxLength property available to you. In order to do this, you need to change the following.
properties: {
value: "",
capsLock: false, // Add a comma here
maxLength: 0 // Add this line
},
document.querySelectorAll(".use-keyboard-input").forEach(element => {
element.addEventListener("focus", () => {
this.open(element.value, element.maxLength, currentValue => { // Pass element.maxLength as a parameter
element.value = currentValue;
});
});
});
open(initialValue, maxlength, oninput, onclose) { // Add maxlength parameter
this.properties.value = initialValue || "";
this.properties.maxLength = maxlength; // Set the property value
this.eventHandlers.oninput = oninput;
this.eventHandlers.onclose = onclose;
this.elements.main.classList.remove("keyboard--hidden");
},
Next you need to go through each button's event handler and add a conditional to it. I'll give you an example here, you can do all the others yourself as it's just the same.
case "enter":
keyElement.classList.add("keyboard__key--wide");
keyElement.innerHTML = createIconHTML("keyboard_return");
keyElement.addEventListener("click", () => {
// Check if the length of the value is equal or bigger than maxLength
if(this.properties.value.length >= this.properties.maxLength){
// Return false so it doesn't do anything
return false;
}
this.properties.value += "\n";
this._triggerEvent("oninput");
});
break;
Just make sure you don't add that to keys that you still want to function in case maxLength is reached.