# InputField : only integer or list of integers in a specific range

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

• 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. Feb 17, 2022 at 9:31
• try Function[If[And[TotalnumberOfnode >= # >= 1, IntegerQ[#]] || And[ListQ[#], TotalnumberOfnode >= Max@# >= 1], nodeNumber = #]] in the second argument of Dynamic?
– kglr
Feb 17, 2022 at 10:52
• @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 = #]]]] Feb 17, 2022 at 12:11

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]

• Work great for single number but not for lists. Feb 18, 2022 at 7:22

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]
]
]