Message Boards Message Boards

0
|
2546 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How enforce a range on an InputField for a Number as a Manipulate control?

Enforcing a min/max range using a Slider is easy. Here a Number is constrained to 0..1:

Manipulate[number, {{number, 0.3, "Number:"}, 0, 1, ControlType -> Slider}]

But when using an InputField control the min/max range seems to be ignored:

Manipulate[number, {{number, 0.3, "Number:"}, 0, 1, ControlType -> InputField}]

This enforces a Number on an InputField but still does not enforce the min/max range:

Manipulate[number, {{number, 0.3, "Number:"}, 0, 1, InputField[#, Number, FieldSize -> 10] &}]

Manipulate with ranged Slider vs InputField as Number

POSTED BY: Darren Kelly
5 Replies

An answer kindly was offered on Stackoverflow buy Kuba. For some reason I can't leave comments their yet (just joined) so I'll respond to here. Kuba's suggestion:

Manipulate[number, 
  { {number, 0.3, "Number:"}, 
    ControlType -> InputField, 
    TrackingFunction :> Function[number = Clip[#, {0, 1}]]
  }
]

This unfortunately does not restrict entry in the field live. It even permits entry of non-numerical alpha characters. It's perhaps slightly better than filtering it in the result before application.

POSTED BY: Darren Kelly
Posted 2 years ago

Crossposted here.

POSTED BY: Rohit Namjoshi

@Rohit Namjoshi

Although I am not new to Mathematica, I have only newly joined both the stack overflow Mathematica forum and Wolfram Mathematica forum group. I did wonder about what Wolfram Research prefers, whether it is best to post here (on the Wolfram Research forum), or on stack overflow, and whether posting the same question on both causes confusion. Glad for advice or a link to the official policy.

POSTED BY: Darren Kelly
Posted 2 years ago

Well, this seems to work, but I'm not entirely happy with it. Seems like there should be a better way to add validation with the InputField itself, but I'd need to learn more about InputField.

Manipulate[
 number = Max[0, Min[number, 1]], 
 {{number, 0.3, "Number:"}, InputField}]
POSTED BY: Eric Rimbey

@Eric Rimbey

I had already thought of squeezing the result using Min and Max, but that is really post-validation and does not integrate well with any live field entry restriction or validation message system.

Seems like there should be a better way to add validation with the InputField itself

Yep. Compare with the FormObject system which does enforce restrictions on submission. This StructuredQuantity approach partially does the job (it doesn't prevent non-numerical keyboard entry into the field though):

"oRefP$kPa" -> <|
"Interpreter" -> Restricted["StructuredQuantity", "Kilopascals", {10., 20.}]
|>

It gives a nice validation warning in red on form submission, with an explanation about the allowed range.

POSTED BY: Darren Kelly
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract