Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.2K Views
|
7 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Conditions on the number entered in InputField?

Posted 4 years ago
POSTED BY: Laurens Wachters
7 Replies
Posted 4 years ago
POSTED BY: Rohit Namjoshi
Posted 4 years ago

Hi Laurens,

You could experiment with something like this

ClearAll[check, val]
check[n_] := Between[n, {5, 10}]

val = 6;
{InputField[Dynamic[val, (If[check[#], val = #, val] &)], Number], Dynamic[val]}

If the input value is between 5 and 10 it will be accepted, if not the old value is retained. This works in a notebook, I have not tested it in a CDF.

POSTED BY: Rohit Namjoshi

Thanks a lot Rohit. This works perfectly! Laurens

POSTED BY: Laurens Wachters
Posted 4 years ago

Hi Laurens,

My mistake, sorry about that. The code to replace larger entries with the max should be

ClearAll[check, val, max]
check[n_] := n <= max
val = 5;
max = 10;
{InputField[Dynamic[val, (If[check[#], val = #, val = max] &)], Number], Dynamic[val]}
POSTED BY: Rohit Namjoshi

Hi Rohit, Thanks a lot for your answer. With Mathematica 11.2 using:the code

ClearAll[check, val, max]
check[n_] := n <= max
val = 5;
max = 10;
{InputField[Dynamic[val, (If[check[#], val = #, max] &)], Number], 
Dynamic[val]}

when I first first enter for instance 4 in the inputfield I get 4 as a result. That is as it should. But when I then enter 11, which is higher than the allowed maximum 10, I get again 4. I had prefered getting 10. But I can live with it.

The code that you suggested for the button case works perfectly.

I combined things in the following code for a CDF: Here the maximum is variable. It depends on the date.

Export["Preventing that input in an InputField exceeds a certain \
value.cdf",
 CreateDocument[ {TextCell[
    "Preventing that input in an InputField exceeds the value of max"],
   DynamicModule[{val, max, check},
    Column[{
      check[n_] := n <= max;
      max = QuantityMagnitude[DateObject[] - DateObject[{2021, 1, 1}]];
      val = max;
      Row[{  
        InputField[Dynamic[val, (If[check[#], val = #, max] &)], 
         Number], Dynamic[Button["-", val--]], 
        Button["+", If[check[val + 1], val++, max]]   }],
      Row[{Dynamic[val]}]    }]]  }]]

This works nicely, but still with not the possiblity of getting the maximally allowed value as a result if a too high value is entered in the input field. You might still have a suggestion for getting this? Then I would be still more happy than I am now.

POSTED BY: Laurens Wachters

Thank you very much, Rohit. I tried your code also in a CDF. It worked nicely. The code returns the previous entry if the next one falls outside the limits. I rebuilt your code for an upper limit as follows:

`ClearAll[check, val, max]
check[n_] := n <= max
val = 5;
max = 10;
{InputField[Dynamic[val, (If[check[#], val = #, val] &)], Number], 
Dynamic[val]}`

Then I tried if it would be possible to get val back to the value of max if this limit is exceeded by replacing the second "val" in the If-statement by "max". So as follows:

ClearAll[check, val, max]
  check[n_] := n <= max
  val = 5;
  max = 10;
  {InputField[Dynamic[val, (If[check[#], val = #, max] &)], Number], 
   Dynamic[val]}`

But this gives the same result as above. So not the value of max. Do you understand why? A further problem in this context concerns the use of Button to change a value in an inputfield. In its most simple way I like to use the following code for this:

ClearAll[val]
val = 5;
{InputField[Dynamic[val], Number], Button[ "-", val--], 
 Button["+", val++], Dynamic[val]}

I tried to use this with an If-statement to stay below a given limit in the following way:

ClearAll[check, val, max]
check[n_] := n <= max
val = 5;
max = 10;
{InputField[Dynamic[val], Number], Button[ "-", val--], 
 Dynamic[Button["+", If[check[#], val = val + 1, val] &]], 
 Dynamic[val]}

But here the + button no longer works.at all. Do you have any suggestion? Thanks in advance, Laurens

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