Function Repository Resource:

ValidatedInputField

Source Notebook

Create an input field that handles erroneous inputs gracefully and responds to the Enter/Return key

Contributed by: Jayanta Phadikar

ResourceFunction["ValidatedInputField"][Dynamic[var],test,msg]

creates an editable input field with dynamically updated field value var, that performs a validity check for the input using test and shows msg in case the test fails.

ResourceFunction["ValidatedInputField"][Dynamic[var],test,msg,action]

creates an input field that performs action when Enter/Return is pressed.

Details

The function accepts an argument test that represents a validity check for the input value. If the input is not valid, an error dialog is displayed containing the message msg.
The input is interpreted as a Wolfram Language expression. Therefore, entering a sequence of letters is interpreted as a symbol name, rather than a string.
An optional task can be specified using action which will be executed after typing in the value and hitting Enter/Return.
ResourceFunction["ValidatedInputField"] accepts most of the options of the built-in InputField.

Examples

Basic Examples (2) 

Create an input field that accepts a number between 0 and 10; trying to enter a value outside the range will result in an error message:

In[1]:=
DynamicModule[{x = 5},
 ResourceFunction["ValidatedInputField"][
  Dynamic[x], (NumberQ[#] && (0 <= # <= 10)) &, "Please enter a number between 0 and 10"]
 ]
Out[1]=

An input field that prints the current number upon hitting Enter/Return:

In[2]:=
DynamicModule[{x = 10},
 ResourceFunction["ValidatedInputField"][Dynamic[x], NumberQ[#] &, "",
   Print[x]]
 ]
Out[2]=

Options (8) 

Appearance (1) 

Change the appearance:

In[3]:=
Table[ResourceFunction["ValidatedInputField"][Dynamic[x], NumberQ[#] &, "", "", Appearance -> a], {a, {"Framed", "Frameless"}}]
Out[3]=

BaselinePosition (1) 

Set the baseline position:

In[4]:=
Row[Table[
  ResourceFunction["ValidatedInputField"][Dynamic[x], NumberQ[#] &, "", "", FieldSize -> 5, BaselinePosition -> p], {p, {Top, Bottom, Center}}], "xxxx"]
Out[4]=

Enabled (1) 

Disable the input field:

In[5]:=
ResourceFunction["ValidatedInputField"][Dynamic[x], NumberQ[#] &, "", "Please enter a number", Enabled -> False]
Out[5]=

FieldMasked (1) 

Mask the contents:

In[6]:=
DynamicModule[{x = 1000},
 ResourceFunction["ValidatedInputField"][Dynamic[x], NumberQ[#] &, "Please enter a number", Echo["Secret number recorded"], FieldMasked -> True]
 ]
Out[6]=

FrameMargins (1) 

Set a larger frame margin:

In[7]:=
ResourceFunction["ValidatedInputField"][Dynamic[x], NumberQ[#] &, "", "Please enter a number", FrameMargins -> 20]
Out[7]=

ImageMargins (1) 

Set a larger image margin:

In[8]:=
Table[Framed@
  ResourceFunction["ValidatedInputField"][Dynamic[x], NumberQ[#] &, "", "Please enter a number", ImageMargins -> m], {m, {0, 30, 50}}]
Out[8]=

FieldSize (1) 

Alter the field size:

In[9]:=
Table[ResourceFunction["ValidatedInputField"][Dynamic[x], NumberQ[#] &, "Please enter a number", Print[x], FieldSize -> s], {s, {3, 5, 10}}]
Out[9]=

ImageSize (1) 

Alter the image size:

In[10]:=
Grid[Table[
  ResourceFunction["ValidatedInputField"][Dynamic[x], NumberQ[#] &, "", "Please enter a number", ImageSize -> {w, h}], {h, {20, 40, 70}}, {w, {30, 50, 70}}]]
Out[10]=

Applications (2) 

Show an error dialog when an entered value for a plot range is not a number or is less than zero:

In[11]:=
DynamicModule[{xr = 10, yr = 10, msg},
 msg = "Please enter a number greater than 0";
 Column[{
   Row[{"Range x: ", ResourceFunction["ValidatedInputField"][
      Dynamic[xr], (NumberQ[#] && # > 0) &, msg]}],
   Row[{"Range y: ", ResourceFunction["ValidatedInputField"][
      Dynamic[yr], (NumberQ[#] && # > 0) &, msg]}],
   Dynamic[
    Plot3D[Sin[x] Cos[y], {x, 0, xr}, {y, 0, yr}, ImageSize -> Medium]]
   }]
 ]
Out[11]=

Show an error dialog when an entered radius is not a number between 1 and 2:

In[12]:=
DynamicModule[{disks, rad = 1.5}, disks = {RandomColor[], Disk[RandomReal[{-18, 18}, 2], 1]}; Column[{
   ResourceFunction["ValidatedInputField"][
    Dynamic[rad],
    (NumberQ[#] && # >= 1 && # <= 2) &,
    "Please enter a number between 1 and 2",
    disks = Append[disks, {RandomColor[], Disk[RandomReal[{-18, 18}, 2], rad]}]
    ],
   Spacer[10],
   Framed[
    Graphics[Dynamic[disks], PlotRange -> {{-20, 20}, {-20, 20}}, ImageSize -> Medium]]
   }]
 ]
Out[12]=

Properties and Relations (2) 

The built-in InputField responds to Enter/Return only when the value is changed. The following prints the value in the input field for a new value only:

In[13]:=
DynamicModule[{x = 10},
 InputField[Dynamic[x, (x = #; Print[x]) &], ImageSize -> {100, 25}]
 ]
Out[13]=

ValidatedInputField alleviates this limitation by printing the value whenever Enter/Return is pressed:

In[14]:=
DynamicModule[{x = 10},
 ResourceFunction["ValidatedInputField"][Dynamic[x], NumberQ[#] &, "",
   Print[x], ImageSize -> {100, 25}]
 ]
Out[14]=

Publisher

Jayanta Kumar Phadikar

Version History

  • 1.0.0 – 04 May 2021

Related Resources

License Information