How to Validate a Number or Phone Field in Shack Forms

In some cases, you may need to ensure that a Shack Forms user can enter only numeric data into a certain field.

  • Open your Shack Forms module for editing.
  • Open the field that needs to me limited only to numeric data.
  • Set the Field type to Text.
  • Enter /^\d+$/into the Validation (Regular Expression) field:

the validation field

  • Click Save or Save & Close.

If I go to my form and enter a letter into this field, both the field label and the field itself get displayed in red color, indicating an input error:

alpha characters flagged as error

Phone numbers: You can expand on the advice above to force people to enter valid phone numbers. For example, use /^\d{9}$/ if you wish to limit your input to a nine-digit number.