1
$\begingroup$

I'm making a dynamic structure which reads some output from a FEM program (node results).

I'd like an inputfield in which I specify the number(s) of the nodes I am interested in. The value of the InputField can only be an Integer or a list of integers in a specific range.

Let's say we have only 10 nodes, then the only values the InputField can take are :

{1,2,3,4,5,6,7,8,9,10}

I want to be sure that the user will not be able to put values like {0,11,Pi,...} and if he does, the InputField returns 1.

It may be something like :

(Which sure not working.)

TotalnumberOfnode = 10;
InputField[Dynamic[nodeNumber, {Integer,Element@Range[TotalnumberOfnode]}]]

(Working one but not for lists)

 TotalnumberOfnode = 10;
 InputField[
  Dynamic[nodeNumber, 
   Function[If[And[TotalnumberOfnode >= # >= 1, IntegerQ[#]], 
    nodeNumber = #]]] 
 ]

Thanks

$\endgroup$
3
  • $\begingroup$ Let me have second look. The parsing is not restting nodenumber since it is dynamic., It is only doing the parsing now. Will delete my answer for now. $\endgroup$
    – Nasser
    Feb 17, 2022 at 9:31
  • $\begingroup$ try Function[If[And[TotalnumberOfnode >= # >= 1, IntegerQ[#]] || And[ListQ[#], TotalnumberOfnode >= Max@# >= 1], nodeNumber = #]] in the second argument of Dynamic? $\endgroup$
    – kglr
    Feb 17, 2022 at 10:52
  • $\begingroup$ @kglr Nice, I modified a bit your solution to avoid the user to be able to insert 0 in a list. However, it is still possible to insert non-integers in a list. InputField[ Dynamic[nodeNumber, Function[If[ And[TotalnumberOfnode >= # >= 1, IntegerQ[#]] || And[ListQ[#], TotalnumberOfnode >= Max@#, Min@# >= 1], nodeNumber = #]]]] $\endgroup$ Feb 17, 2022 at 12:11

2 Answers 2

1
$\begingroup$

Here's another option. Might want to improve it to handle Null better.

InputField[
  Dynamic[
    num, 
    (num = First[Nearest[Range[TotalnumberOfnode], #, 1]]) &], 
  Number, 
  ContinuousAction -> True]
$\endgroup$
1
  • $\begingroup$ Work great for single number but not for lists. $\endgroup$ Feb 18, 2022 at 7:22
0
$\begingroup$

I'm not sure this is the most elegant way but it does the job :

InputField[
Dynamic[nodeNumber, 
  Function[
   If[
    Or[
    And[TotalnumberOfnode >= # >= 1, IntegerQ[#]], 
    And[TotalnumberOfnode >= Max@#, Min@# >= 1,ListQ[#],VectorQ[#, IntegerQ]
  ]
 ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.