[SOLVED] Input validation on lemur TextField

Does somebody already a input validation for lemurs TextField? I want to react for example if somebody does type “hello” instead a number. My first idea I have ist to use a control, but maybe there is a much simpler solution?
I thought of control to react as you type (delete it instantly). Ideas?

You can snag the validation from my old library if you don’t want to write it yourself. Someone has the sources available somewhere.

1 Like

I do it at the moment this way

package ch.artificials.bg;

import com.simsilica.lemur.TextField;

public class NumberField extends TextField implements GetText {
    
    public NumberField(String text) {
        super(text);
    }

    @Override
    public void updateLogicalState(float tpf) {
        try {
            Integer.parseInt(getText());
        } catch (NumberFormatException e) {
            setText("");
        }
        super.updateLogicalState(tpf);
    }
}

If I type something else than a parsable number I delete the text in the text field. No clue if that is the best way.

@t0neg0d it is less about validation itself but more about on the fly reaction if someone types something wrong (like deleting the complete text or just the new added char). But maybe Lemur has something on board which could do what I want easier (ok it is not much code, so my solution looks also easy but my SDK tells me that I should actually not call this update method ;))

My old library allowed you to enforce any number of standard rules (alpha, alphanumeric, numeric, etc) and also allowed you to add custom validation and reaction to that validation. I’m totally unfamiliar with Lemur, but if what you are doing works, what is it you’re looking for?

Two things I found the above solution after my question that’s why I did post it in my first response. Second it behaves a bit odd as while I type it I see the wrong key strokes for a fraction of time and I thought that maybe somebody else did the same in a more elegant way (elegant in code and visibility).
But you are right it works so far so it it solved. Thanks for listening.

I probably wouldnt use setText ("") as if you type in something “long” and the Text is gone because my fat fingers hit a number you are annoyed that the text is gone.

Maybe some indication like a red frame fits your usecase and it would also solve flashing characters which is on the other Hand common as it signalizes that there was a Validation fault instead of a broken keyboard

2 Likes

There are a few ways to get different “validation” effects depending on what you are really after. For example, you can filter all text entry if you like with some caveats by using DocumentModelFilter (of which there are some standard filters in TextFilters).

If you just want to highlight an error when it’s wrong then there are a few ways. Either the way you are doing it, only on focus lost, or maybe as an action listener.

I want to ignore all “wrong” key strokes for a given field. Mainly nummeric fields where something else would cause a Integer/Long/Float parse exception. I think my ugly hack is exactlythat an ugly hack.
If there is a better way and maybe an example let me know.

Run the validation on the next input character prior to appending it to the visible string?

This (if I remember correctly) was how I handled input validation… it validated the keyboard input not the string shown in the textfield.

EDIT: Which allows you to write a single validation routine and pass it a grabbag of specific characters to allow/disallow, or character type (alpha/numeric/etc)

1 Like

See how PasswordField does it to support filtering:

…just ignore the part where it also overrides the output character.

1 Like

Perfect!

1 Like