[Resolved]Number Text Field

Hi All,



I’m wanting to do number entry from a user (i.e. send 1002 gold to X). At the very minimum I want to restrict their text entry to be numbers only. Ideally I’d like to go one further and limit the minimum and maximum values entered as they are typing.



Does anyone know of anything in nifty that does this at the moment?



Thanks,

Z

Since you’re already using the latest Nifty 1.3.2-SNAPSHOT build :wink: you should be able to use the new Textfield filter options to restrict/format the input. This is not yet fully documented (wiki) but the new Textfield control is fully documented with JavaDoc so it should be pretty obvious how you should use this feature. See the following links for details:



https://github.com/void256/nifty-gui/blob/1.3/nifty-controls/src/main/java/de/lessvoid/nifty/controls/TextField.java

https://github.com/void256/nifty-gui/tree/1.3/nifty-controls/src/main/java/de/lessvoid/nifty/controls/textfield/filter/input

2 Likes

I achieve similar by checking the value after it has been set by the user, and reverting back if their entry is invalid:



[java]

float oldVal = 0;



@NiftyEventSubscriber(pattern=“textFieldPrefix_.*”)

public void textFieldValueChanged(final String id, final TextFieldChangedEvent event) {

float val = oldVal;

try {

val = Float.valueOf(event.getText());

} catch (Exception e) {}

if (val == Float.NaN) {

val = oldValue;

}



handeUpdateValue(id.replace(“textFieldPrefix_”, “”), val);

oldVal = val;

}

[/java]



(this example is for handling float inputs, and I hacked it up in here so syntax may be off) … It’s far from the cleanest solution but it does work, and gives me full control over field validation.

1 Like

Yeeh, that was going to be my fallback solution @thetoucher. Does that respond immediately they press a key or is it like the swing ones where you can only validate once they have finished entering? The user interface is much more friendly if it gives immediate feedback on bad entry.



It looks like a filter for digits would give me most of what I need - and then I just validate the entered value and cap it (i.e. negative becomes 0 >max becomes max).



I’ll see how it goes :slight_smile:



Thanks guys.

@zarch immediately yes, as soon as the value in the text field is changed.

1 Like
@thetoucher said:
@zarch immediately yes, as soon as the value in the text field is changed.


Just curious (I've done something similar for Swing) - don't you have problems for float values (say -0.1f) in that user could type '-' and that isn't a valid float until you type '0'? I never did solve that until I went for regexp-matching.

@jmaasing in the example above, perhaps, never used it. I haven’t encountered those issues, yet. I would probably default to regular expressions when looking for a fix though :slight_smile:

@jmaasing said:
Just curious (I've done something similar for Swing) - don't you have problems for float values (say -0.1f) in that user could type '-' and that isn't a valid float until you type '0'? I never did solve that until I went for regexp-matching.

Talking to myself here but anyway :)

It isn't rocket science but if I recall I think I ended up with a filter like those void256 link (that ignores keys that are meaningless), and a visual cue (red frame around the input field) while the input did not validate (like thetoucher example with parsing it for every keypress) and finally a validation message when the user tried to "apply" the values explaining that '-,.' isn't a valid number even if the regexp thought so :)

(Also, i18n is tricky what with , or . used as decimal and/or grouping and should ' ' space be allowed)

I guess what I'm driving at is that those are things to think about when dealing with user experience for inputting numbers.

Fortunately all I’m looking to filter for is positive integers so an isDigit filter on the key entry combined with a check on the max value and I’m sorted :slight_smile:

Works perfectly, thanks guys :slight_smile: