As described in ScriptUI reference, there are 2 ways to implement changing listener on EditText
:
editText.onChanging = function() {
...
}
editText.addEventListener('changing', function(event) {
...
})
However, the function is triggered after an edit to EditText.text
has been made, denying opportunity to obtain old text value as a fallback option is new value is undesired. I'm looking for something like this:
editText.onChanging = function(oldValue) {
var newValue = this.text
if (!isCorrect(newValue)) {
this.text = oldValue
}
}
editText boxes have an onActivate callback which is triggered when the element gets focus. If you store the current text value of the editText at that point, it can be returned to its original state if the new value doesn't validate.
Note that it might be worth thinking about the UX here—if you enter an invalid value, and the value just reverts, you might not realise that it has done so. For the user this could be very confusing: it looks like I enter a value, but nothing happens. It might be better to throw an error, such as a dialog, or disable other controls, e.g. grey out the "do the things" button if the parameters are incorrect.