In any web form, validation is essential to prevent users from entering the wrong data, Magento provides a few predefined rules for the validation and we can create our own rule as well. These rules we can apply as a class name, In this article, I will share List of Validation classes in Magento 2 that you can use to validate any frontend or admin form and admin store configuration as well.

In Magento 2, validation classes are utilized to validate various data inputs within the system. These validation classes help ensure that data entered by users or passed through the system meets certain criteria or constraints, thus maintaining data integrity and security.

List of validation classes in Magento 2:

Class/RuleDescription
alphanumericAllow Letters, numbers, spaces, or underscores only
letters-onlyAllow Letters only
max-wordsAllow the maximum number of  words that you want
min-wordsAllow the minimum number of words that you want
validate-numberValidate the number in the input fields, it ensures that the data entered by the user is a valid number.
validate-no-html-tagsThis rule is used to ensure that a given input does not contain any HTML tags.
validate-selectThis rule ensures that a valid option is selected from the dropdown list
validate-no-emptyThe rule is used for form field validation to ensure that a field is not left empty
validate-emailThis rule ensures that the input provided by the user matches the standard format of an email address
validate-digitsThis rule is used to validate that a given value contains only digits (0-9)
validate-cc-typeThis rule is used for credit card validation. It ensures that the credit card type entered by the user matches one of the accepted card types configured in the system
validate-cc-expThis rule is used to validate credit card expiration dates. This rule ensures that the expiration date entered by the user is valid and falls within acceptable ranges
required-entryIt is primarily used to ensure that a form field cannot be submitted empty or with only whitespace characters.
validate-digits-rangeThis rule is used to ensure that a numerical value falls within a specified range of digit
no-whitespaceThis rule checks whether a given input contains any whitespace characters
mobileUKThis validation rule is specifically designed to validate UK mobile phone numbers.
phoneUKThis validation rule is specifically designed to validate phone numbers according to the format used in the United Kingdom (UK)
not-negative-amountThis rule is used to ensure that a given numerical value is not negative.
validate-dobThis rule is used to validate date of birth (DOB) inputs. This rule ensures that the entered value is a valid date and conforms to certain criteria such as format and range.
timeThis rule ensures that the time value provided by the user or system adheres to a specific format or meets certain criteria.
integerThis validation rule is used to ensure that a given input value is an integer (a whole number without any decimal or fractional part)
dateNLThis rule is a validation rule specifically designed to validate dates in the Dutch date format. This format follows the convention of “dd-MM-yyyy”

“dd” represents the day of the month (01 to 31)
“MM” represents the month (01 to 12)
“yyyy” represents the year (e.g., 2024)
validate-streetThis rule is used to validate street addresses entered by users. This validation rule ensures that the street address meets certain criteria, such as length constraints and character types.

You can implement all the above validation rules by adding a class name of specific input.

I hope this article proves to be beneficial for you. Thank you for reading and stay tuned for more insightful content!

Rate this post