java - JavaFX Input Validation Textfield -


i'm using javafx , scene builder , have form textfields. 3 of these textfields parsed strings doubles.

i want them school marks should allowed between 1.0 , 6.0. user should not allowed write "2.34.4" "5.5" or "2.9" ok.

validation parsed fields:

public void validate(keyevent event) {     string content = event.getcharacter();     if ("123456.".contains(content)) {             // no numbers smaller 1.0 or bigger 6.0 - how?     } else {         event.consume();     } } 

how can test if user inputs correct value?

i searched on stackoverflow , on google didn't find satisfying solution.

textfield.focusedproperty().addlistener((arg0, oldvalue, newvalue) -> {         if (!newvalue) { //when focus lost             if(!textfield.gettext().matches("[1-5]\\.[0-9]|6\\.0")){                 //when not matches pattern (1.0 - 6.0)                 //set textfield empty                 textfield.settext("");             }         }      }); 

you change pattern [1-5](\.[0-9]){0,1}|6(.0){0,1} 1,2,3,4,5,6would ok (not 1.0,2.0,...)

update here small test application values 1(.00) 6(.00) allowed:

public class javafxsample extends application {  @override public void start(stage primarystage) {     primarystage.settitle("enter number , hit button");     gridpane grid = new gridpane();     grid.setalignment(pos.center);     label label1to6 = new label("1.0-6.0:");     grid.add(label1to6, 0, 1);     textfield textfield1to6 = new textfield();      textfield1to6.focusedproperty().addlistener((arg0, oldvalue, newvalue) -> {         if (!newvalue) { // when focus lost                 if (!textfield1to6.gettext().matches("[1-5](\\.[0-9]{1,2}){0,1}|6(\\.0{1,2}){0,1}")) {                     // when not matches pattern (1.0 - 6.0)                     // set textfield empty                     textfield1to6.settext("");                 }             }         });     grid.add(textfield1to6, 1, 1);     grid.add(new button("hit me!"), 2, 1);     scene scene = new scene(grid, 300, 275);     primarystage.setscene(scene);     primarystage.show(); }  public static void main(string[] args) {     launch(args); }  } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -