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/Rule | Description |
alphanumeric | Allow Letters, numbers, spaces, or underscores only |
letters-only | Allow Letters only |
max-words | Allow the maximum number of words that you want |
min-words | Allow the minimum number of words that you want |
validate-number | Validate the number in the input fields, it ensures that the data entered by the user is a valid number. |
validate-no-html-tags | This rule is used to ensure that a given input does not contain any HTML tags. |
validate-select | This rule ensures that a valid option is selected from the dropdown list |
validate-no-empty | The rule is used for form field validation to ensure that a field is not left empty |
validate-email | This rule ensures that the input provided by the user matches the standard format of an email address |
validate-digits | This rule is used to validate that a given value contains only digits (0-9) |
validate-cc-type | This 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-exp | This 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-entry | It is primarily used to ensure that a form field cannot be submitted empty or with only whitespace characters. |
validate-digits-range | This rule is used to ensure that a numerical value falls within a specified range of digit |
no-whitespace | This rule checks whether a given input contains any whitespace characters |
mobileUK | This validation rule is specifically designed to validate UK mobile phone numbers. |
phoneUK | This validation rule is specifically designed to validate phone numbers according to the format used in the United Kingdom (UK) |
not-negative-amount | This rule is used to ensure that a given numerical value is not negative. |
validate-dob | This 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. |
time | This rule ensures that the time value provided by the user or system adheres to a specific format or meets certain criteria. |
integer | This validation rule is used to ensure that a given input value is an integer (a whole number without any decimal or fractional part) |
dateNL | This 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-street | This 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!