Usage
The @mantine/form package exports several functions that can be used in validation rules object.
Validation functions are tiny in size and provide basic validation. If you have complex validation requirements, use other types of validation.
Optional error
The last argument of all validator functions below is optional. If the error is not set, then fields with failed validation will only have invalid styles without an error message:
isNotEmpty
isNotEmpty checks that the form value is not empty. Empty string, empty array, false, null and undefined
values are considered to be empty. Strings are trimmed before validation.
isEmail
isEmail uses /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,})+$/ regexp to determine whether the form value is an email:
matches
matches checks whether the form value matches the given regexp. If the form value is not a string, validation will fail.
isInRange
isInRange checks whether the form value is within the given min-max range. If the form value is not a number, validation will fail.
hasLength
hasLength checks whether the form value length is within the given min-max range.
hasLength will work correctly with strings, arrays and any other objects that have a length property.
Strings are trimmed before validation.
matchesField
matchesField checks whether the form value is the same as the value in another form field.
Note that matchesField can only work with primitive values (arrays and objects cannot be compared).
isJSONString
isJSONString checks whether the form value is a valid JSON string.
isUrl
isUrl checks whether the form value is a valid URL. By default, only http and https protocols are allowed
and localhost is rejected. You can customize this behavior by passing options as the first argument.
isOneOf
isOneOf checks whether the form value is included in the given list of allowed values.
Uses strict equality for comparison.
isNotEmptyHTML
isNotEmptyHTML checks that the form value is not an empty HTML string. Empty string, string with only HTML tags and whitespace are considered to be empty.