3

I am using a NumberTextField in Wicket 7 and my requirements are

  1. User should be able to enter only a whole numeric char into the textfield and neither a decimal value nor any non-numeric char.
  2. char should not be more than 3 characters long.

But NumberTextField allows the user to input a decimal value. How do I restrict the length of NumberTextField in Wicket 7.

thg
  • 1,209
  • 1
  • 14
  • 26
nitin
  • 83
  • 1
  • 9

1 Answers1

1

You need to set the type of the field. You can do it in the constructor: NumberTextField(String id, IModel model, Class type) or via setter: field.setType(Integer.class).

Then you can add validator for the allowed values: field.add(RangeValidator.range(0, 999).

If you need to have the same validations at the client side then you will need to use JavaScript validation library. You can check https://github.com/code-troopers/wicket-jsr303-parsley and the blog: http://wicketinaction.com/2013/04/server-and-client-side-validation/

martin-g
  • 17,243
  • 2
  • 23
  • 35
  • 1
    For a NumberTextField instead of adding a `RangeValidator` yourself I would rather use the existing methods `setMinimum` and `setMaximum`, which will add a `RangeValidator` to the input and also the corresponding html5 attributes. – OH GOD SPIDERS Feb 06 '18 at 12:41
  • @martin-g user is able to enter decimal value. – nitin Feb 06 '18 at 13:46
  • To prevent entering decimal value in the input field you should use JavaScript plugin like https://igorescobar.github.io/jQuery-Mask-Plugin/ . Wicket is server side framework and the suggestions above are only to validate that data sent to the server is valid before storing it to the database or executing the business logic. – martin-g Feb 06 '18 at 14:50