Message Boards Message Boards

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

Conditions on the number entered in InputField?

Posted 3 years ago

I am looking for a simple way to show that a number that I have entered in an InputField in a CDF is higher or lower than acceptible, and to make sure that in the CDF the entered number is not used for further calculations. The only result of evaluation might for instance be that in the InpuField the same number is shown as before the new number was entered. An other possibility is that in the InputField the text "too high" or "too low" is shown. Thanks in advance!.

POSTED BY: Laurens Wachters
7 Replies

Welcome to Wolfram Community!

Please make sure you know the rules: https://wolfr.am/READ-1ST

Please, show your own initial effort with the Wolfram Language code. You can edit your post and add the code using the formatting button. You can also embed notebook or attach notebook. Thank you.

enter image description here

POSTED BY: Moderation Team
Posted 3 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

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 3 years ago

Hi Laurens,

I am unable to reproduce the problem in this example. It works fine on 12.2.0 for Mac OS X x86 (64-bit) (December 12, 2020)

ClearAll[check, val, max]
check[n_] := n <= max
val = 5;
max = 10;

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

For the button example this works

{InputField[Dynamic[val], Number], Dynamic[Button["-", val--]], 
 Button["+", If[check[val + 1], val++, val]], 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
Posted 3 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

Thanks a lot Rohit. This works perfectly! Laurens

POSTED BY: Laurens Wachters
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