I would like to check the value of cp5 textfields as soon as they lose focus. The only thing I found regarding this, it that textfields have an .isFocus() method, but I cant think of a smart way to utilize this.
You would have to register, when a textfield has focus and then check if if this textfield loses its focus again. This method also has to work for multiple fields.
Edit:
After I have been thinking about this for a bit, I have become quite sure that there is no better solution than the one I’ll describe:
- check if any Textfields have focus (Textfield.isFocus:boolean)
- if yes, save the name or whatever of the textfield
- keep checking if the textfield has focus
- if it loses focus, check value
To clarify: I’m not asking for a working code of the described above, I’m just asking for a better approach.
Edit 2:
I solved this problem by simplifying it. Am going to add my code later.
Edit 3:
A few days after asking this question, i cant comprehend what I was thinking at times. I think i must have gotten confused by the examples and descriptions given in the cp5 documentation about textfields, because I was starting to implement the idea I listed above, when I noticed that I was thinking in opposites for some reason.
I was trying to check when a textfield loses focus, instead of just checking if a textfield has focus. Instead of checking the user-input after the textfield lost focus, I’m now checking while it has focus.
Basically, if it hasnt gotten clear yet:
if (option1.isFocus()) {
if (!option1.getText().matches("[0-9]+")) {
// show an error
} else {
// you get the point
}
}
(option1 being the textfield)