Validators class contains static methods that perform the actual validation logic used by the validation rules. These methods can be called directly for simple boolean validation checks without needing to create a Validator instance.
Import
When to Use
Use these static methods when you:- Need a simple true/false validation result
- Want to perform validation without exception handling
- Need to build conditional logic based on validation results
- Want to create custom validation logic
Length Validators
required
Validates that the String is not null and not empty.length
Validates that the String has an exact length of characters.minLength
Validates that the length of the String is not less than the minimum.maxLength
Validates that the length of the String is not greater than the maximum.rangeLength
Validates that the length of the String is within the specified range.Format Validators
regExp
Validates that the String matches a regular expression pattern.number
Validates that the String is in numeric format (includes integers, decimals, and negative values).link
Validates that the String is a valid URL (any format: www, http, https).wwwLink
Validates that the String is a URL starting with www.httpLink
Validates that the String is a URL starting with http://httpsLink
Validates that the String is a URL starting with https://ip
Validates that the String is a valid IP address (IPv4 or IPv6).ipv4
Validates that the String is a valid IPv4 address.ipv6
Validates that the String is a valid IPv6 address.time
Validates that the String is in time format (12-hour or 24-hour).time12
Validates that the String is in 12-hour time format with AM/PM.time24
Validates that the String is in 24-hour time format.numberPattern
Validates that the String matches a pattern where ‘x’ represents variable digits.date
Validates that the String matches a specified date format.name
Validates that the String is a proper name (English letters only, spaces allowed).regExp with RegularExpression.NAME_ES.
Content Validators
shouldOnlyContain
Validates that the String only contains characters from the specified alphabet.onlyNumbers
Validates that the String contains only numeric characters.onlyLetters
Validates that the String contains only English letters.onlyAlphanumeric
Validates that the String contains only alphanumeric characters (English letters and numbers).notContain
Validates that the String does not contain any characters from the specified alphabet.mustContainOne
Validates that the String contains at least one character from the specified alphabet.mustContainMin
Validates that the String contains at least a minimum number of characters from the specified alphabet.Value Validators
maxValue
Validates that the numeric value is not greater than the maximum.minValue
Validates that the numeric value is not less than the minimum.rangeValue
Validates that the numeric value is within the specified range.Date Validators
minAge
Validates that the age calculated from the date is greater than or equal to the minimum age.expirationDate
Validates that the date has not expired (is in the past).Practical Examples
Building Custom Validation Logic
Conditional Validation
Password Strength Checker
Age-Restricted Content
Form Field Validation
Real-time Input Validation
Combining with Validation Rules
While these static methods are useful for quick checks, consider using the Validator class when you need:- Error messages
- Multiple validation rules on the same field
- Exception-based validation flow
Related
- Validator - High-level validation with error messages
- String Rules - String validation rules
- Format Rules - Format validation rules
- Alphabets - Predefined character sets
- Regular Expressions - Predefined regex patterns
- Exceptions - Exception handling in validation