In this post I am going to explain how to add validation in system configuration in Magento 2.

In order to do validatetion in system configuration field, you just need to add “validate” tag for field in system.xml file and pass validation class in it.

See the below example of required field validation and email validation.

 

One field can have one or more validator-classes. You just need to add space between classes.

<validate>required-entry validate-email</validate>

The following validation rules are available:

validate-no-html-tags -> HTML tags are not allowed.
validate-select -> Select an option.
validate-no-empty -> Empty Value
validate-alphanum-with-spaces -> Use letters (a-z or A-Z), numbers (0-9), or spaces only in this field.
validate-data -> Use letters (a-z or A-Z), numbers (0-9), or underscores (_) only in this field. The first character should be a letter.
validate-street -> Use letters (a-z or A-Z), numbers (0-9), spaces, and “#” only in this field.
validate-phoneStrict -> Enter a valid phone number. For example, (123) 456-7890 or 123-456-7890.
validate-phoneLax -> Enter a valid phone number. For example, (123) 456-7890 or 123-456-7890.
validate-fax -> Enter a valid fax number. For example, 123-456-7890.
validate-email -> Enter a valid email address. For example, [email protected]
validate-emailSender -> Enter a valid email address. For example, [email protected]
validate-password -> Enter 6 or more characters. Leading and trailing spaces will be ignored.
validate-admin-password -> Enter 7 or more characters, using both numeric and alphabetic.
validate-url -> Enter a valid URL. Protocol is required (http://, https:// or ftp://).
validate-clean-url -> Enter a valid URL. For example, http://www.example.com or www.example.com.
validate-xml-identifier -> Enter a valid XML-identifier. For example, something_1, block5, id-4.
validate-ssn -> Enter a valid social security number. For example, 123-45-6789.
validate-zip-us -> Enter a valid ZIP code. For example, 90602 or 90602-1234.
validate-date-au -> Use this date format: dd/mm/yyyy. For example, 17/03/2006 for the 17th of March, 2006.
validate-currency-dollar -> Enter a valid $ amount. For example, $100.00.