Assume abstract validator callback that returns true of false depending on whether new string value is valid:
function isValid(text) {
/* arbitrary code, returns true or false */
}
I want to apply this callback in input field in HTML/JavaScript. The code in the validator can be any kind of validator.
I've seen people use keyup
event to prevent some characters to be entered. This is flawed method and obviously cannot validate whole input field.
I tried to cancel input
event, that doesn't work:
myInputField.addEventListener("input", function(e) {
console.log(e);
e.preventDefault();
return false;
});
In most GUI frameworks I encountered (Java Swing, C++ Qt) this was as simple as assigning validator callback on the input field. How do I do this in JavaScript?