Skip to main content

Overview

Format rules validate that String values match specific format patterns such as email addresses, dates, URLs, IP addresses, and custom regular expressions.

Email Validation

email

Validates that the String has a valid email format.
public void email(String message)
public void email()
message
String
Custom error message (optional - uses default if not provided)
Example:
Validator validator = new Validator.Builder()
    .build();
validator.email()
    .build();
validator.email("Please enter a valid email address");

validator.isValid("[email protected]");     // true
validator.isValid("[email protected]");  // true
validator.isValid("invalid.email");        // false
validator.isValid("@example.com");         // false
Builder Usage:
Validator emailValidator = new Validator.Builder()
    .required()
    .email("Invalid email format")
    .maxLength(255)
    .build();
Annotation Usage:
public class User {
    @Email
    private String email;
    
    @Email(message = "Please provide a valid email")
    private String alternateEmail;
}

Date and Time Validation

date

Validates that the String matches a specified date format.
public void date(String format, String message)
public void date(String format)
format
String
required
Date format pattern (e.g., “yyyy-MM-dd”, “MM/dd/yyyy”, “dd-MM-yyyy”)
message
String
Error message (can use %s placeholder for format)
Example:
validator.date("yyyy-MM-dd");
validator.date("MM/dd/yyyy", "Date must be in format %s");

// For format "yyyy-MM-dd"
validator.isValid("2024-12-31");  // true
validator.isValid("12/31/2024");  // false
validator.isValid("2024-13-01");  // false (invalid month)

// For format "MM/dd/yyyy"
validator.isValid("12/31/2024");  // true
validator.isValid("2024-12-31");  // false
Common Date Formats:
// ISO 8601
validator.date("yyyy-MM-dd");

// US Format
validator.date("MM/dd/yyyy");

// European Format
validator.date("dd/MM/yyyy");

// With time
validator.date("yyyy-MM-dd HH:mm:ss");
Annotation Usage:
public class Event {
    @Date(format = "yyyy-MM-dd")
    private String eventDate;
    
    @Date(
        format = "MM/dd/yyyy",
        message = "Birth date must be in format MM/dd/yyyy"
    )
    private String birthDate;
}

expirationDate

Validates that the entered date has not expired (is in the future or today).
public void expirationDate(String format, String message)
public void expirationDate(String format)
format
String
required
Date format pattern describing the date format
message
String
Error message (can use %s placeholder for format)
Warning: This function uses the current date of the device. Note: It is recommended to use the date() rule first to ensure valid date format. Example:
validator.date("yyyy-MM-dd");
validator.expirationDate("yyyy-MM-dd");
validator.expirationDate("MM/dd/yyyy", "Card has expired");

// Assuming today is 2024-06-15
validator.isValid("2024-06-15");  // true (today)
validator.isValid("2024-12-31");  // true (future)
validator.isValid("2024-01-01");  // false (past)
Credit Card Example:
Validator cardExpiryValidator = new Validator.Builder()
    .required("Expiration date is required")
    .date("MM/yy", "Invalid date format")
    .expirationDate("MM/yy", "Card has expired")
    .build();
Annotation Usage:
public class CreditCard {
    @Date(format = "MM/yyyy")
    @ExpirationDate(format = "MM/yyyy", message = "Card has expired")
    private String expiryDate;
}

minAge

Validates that the period from the entered date to the current date is greater than or equal to a minimum age.
public void minAge(String format, int age, String message)
public void minAge(String format, int age)
format
String
required
Date format pattern for the birth date
age
int
required
Minimum age required (in years)
message
String
Error message (can use %s placeholder for age)
Warning: This function uses the current date of the device. Note: It is recommended to use the date() rule first. Example:
validator.date("yyyy-MM-dd");
validator.minAge("yyyy-MM-dd", 18);
validator.minAge("MM/dd/yyyy", 21, "Must be at least %s years old");

// Assuming today is 2024-06-15
validator.isValid("2006-06-15");  // true (exactly 18)
validator.isValid("2000-01-01");  // true (24 years old)
validator.isValid("2010-01-01");  // false (only 14)
Registration Form Example:
Validator birthDateValidator = new Validator.Builder()
    .required("Birth date is required")
    .date("yyyy-MM-dd", "Invalid date format")
    .minAge("yyyy-MM-dd", 18, "Must be at least %s years old to register")
    .build();
Annotation Usage:
public class UserRegistration {
    @Date(format = "yyyy-MM-dd")
    @MinAge(format = "yyyy-MM-dd", age = 18, message = "Must be 18 or older")
    private String birthDate;
    
    @Date(format = "MM/dd/yyyy")
    @MinAge(format = "MM/dd/yyyy", age = 21)
    private String driversLicenseDate;
}

time

Validates that the String is in a valid time format (accepts both 12-hour and 24-hour formats).
public void time(String message)
public void time()
Example:
validator.time();
validator.time("Please enter a valid time");

validator.isValid("14:30");      // true (24-hour)
validator.isValid("2:30 PM");    // true (12-hour)
validator.isValid("02:30:00");   // true
validator.isValid("25:00");      // false
validator.isValid("invalid");    // false
Annotation Usage:
public class Appointment {
    @Time
    private String appointmentTime;
}

time12

Validates that the String is in 12-hour time format (with AM/PM).
public void time12(String message)
public void time12()
Example:
validator.time12();
validator.time12("Time must be in 12-hour format");

validator.isValid("2:30 PM");    // true
validator.isValid("12:00 AM");   // true
validator.isValid("11:59 PM");   // true
validator.isValid("14:30");      // false (24-hour format)
validator.isValid("13:00 PM");   // false (invalid)
Annotation Usage:
public class Schedule {
    @Time12
    private String startTime;
    
    @Time12(message = "End time must be in 12-hour format (e.g., 2:30 PM)")
    private String endTime;
}

time24

Validates that the String is in 24-hour time format.
public void time24(String message)
public void time24()
Example:
validator.time24();
validator.time24("Time must be in 24-hour format");

validator.isValid("00:00");      // true
validator.isValid("14:30");      // true
validator.isValid("23:59");      // true
validator.isValid("2:30 PM");    // false (12-hour format)
validator.isValid("24:00");      // false (invalid)
Annotation Usage:
public class FlightSchedule {
    @Time24
    private String departureTime;
    
    @Time24(message = "Arrival time must be in 24-hour format (HH:mm)")
    private String arrivalTime;
}
Validates that the String is a valid link/URL format.
public void link(String message)
public void link()
Example:
validator.link();
validator.link("Please enter a valid URL");

validator.isValid("https://example.com");       // true
validator.isValid("http://example.com");        // true
validator.isValid("www.example.com");           // true
validator.isValid("ftp://files.example.com");   // true
validator.isValid("not a link");                // false
Annotation Usage:
public class SocialProfile {
    @Link
    private String website;
    
    @Link(message = "Invalid portfolio URL")
    private String portfolioUrl;
}

Validates that the String is a link with www format.
public void wwwLink(String message)
public void wwwLink()
Example:
validator.wwwLink();
validator.wwwLink("Link must start with www");

validator.isValid("www.example.com");           // true
validator.isValid("www.subdomain.example.com"); // true
validator.isValid("https://example.com");       // false
validator.isValid("example.com");               // false
Annotation Usage:
@WwwLink
private String website;

Validates that the String is a link with http:// protocol.
public void httpLink(String message)
public void httpLink()
Example:
validator.httpLink();
validator.httpLink("Link must use HTTP protocol");

validator.isValid("http://example.com");   // true
validator.isValid("http://api.example.com"); // true
validator.isValid("https://example.com");  // false
validator.isValid("www.example.com");      // false
Annotation Usage:
@HttpLink
private String apiEndpoint;

Validates that the String is a link with https:// protocol.
public void httpsLink(String message)
public void httpsLink()
Example:
validator.httpsLink();
validator.httpsLink("Link must use secure HTTPS protocol");

validator.isValid("https://example.com");       // true
validator.isValid("https://secure.example.com"); // true
validator.isValid("http://example.com");        // false
validator.isValid("www.example.com");           // false
Annotation Usage:
public class SecuritySettings {
    @HttpsLink(message = "Webhook URL must use HTTPS")
    private String webhookUrl;
}

IP Address Validation

ip

Validates that the String is a valid IP address (IPv4 or IPv6).
public void ip(String message)
public void ip()
Example:
validator.ip();
validator.ip("Please enter a valid IP address");

validator.isValid("192.168.1.1");                    // true (IPv4)
validator.isValid("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); // true (IPv6)
validator.isValid("256.1.1.1");                      // false
validator.isValid("not an ip");                      // false
Annotation Usage:
public class NetworkConfig {
    @Ip
    private String serverAddress;
}

ipv4

Validates that the String is a valid IPv4 address.
public void ipv4(String message)
public void ipv4()
Example:
validator.ipv4();
validator.ipv4("Please enter a valid IPv4 address");

validator.isValid("192.168.1.1");     // true
validator.isValid("10.0.0.1");        // true
validator.isValid("255.255.255.255"); // true
validator.isValid("256.1.1.1");       // false
validator.isValid("2001:0db8::1");    // false (IPv6)
Annotation Usage:
public class ServerConfig {
    @Ipv4
    private String ipAddress;
    
    @Ipv4(message = "Gateway must be a valid IPv4 address")
    private String gateway;
}

ipv6

Validates that the String is a valid IPv6 address.
public void ipv6(String message)
public void ipv6()
Example:
validator.ipv6();
validator.ipv6("Please enter a valid IPv6 address");

validator.isValid("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); // true
validator.isValid("2001:db8::1");                            // true
validator.isValid("::1");                                    // true (localhost)
validator.isValid("192.168.1.1");                            // false (IPv4)
Annotation Usage:
public class NetworkSettings {
    @Ipv6
    private String ipv6Address;
    
    @Ipv6(message = "DNS server must be a valid IPv6 address")
    private String dnsServer;
}

Regular Expression

regExp

Validates that the String matches a custom regular expression pattern.
public void regExp(String regExp, String message)
public void regExp(String regExp)
regExp
String
required
Regular expression pattern to match
message
String
Error message (can use %s placeholder for the regex pattern)
Example:
// Username: letters, numbers, underscore, 3-20 chars
validator.regExp("^[a-zA-Z0-9_]{3,20}$");
validator.regExp("^[a-zA-Z0-9_]{3,20}$", "Invalid username format");

// Hex color code
validator.regExp("^#[0-9A-Fa-f]{6}$", "Must be a valid hex color");

// UUID
validator.regExp(
    "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
    "Must be a valid UUID"
);
Common Patterns:
// Alphanumeric with underscores
validator.regExp("^[a-zA-Z0-9_]+$");

// Lowercase letters only
validator.regExp("^[a-z]+$");

// Letters and spaces
validator.regExp("^[a-zA-Z ]+$");

// US ZIP code
validator.regExp("^\\d{5}(-\\d{4})?$");

// Credit card (basic)
validator.regExp("^\\d{4}-\\d{4}-\\d{4}-\\d{4}$");
Annotation Usage:
public class CustomValidation {
    @RegExp(regExp = "^[A-Z]{2}\\d{6}$")
    private String licenseNumber;
    
    @RegExp(
        regExp = "^#[0-9A-Fa-f]{6}$",
        message = "Color must be a valid hex code (e.g., #FF5733)"
    )
    private String favoriteColor;
}

Complete Examples

Registration Form

public class RegistrationForm {
    @Required
    @Email
    private String email;
    
    @Required
    @Date(format = "yyyy-MM-dd")
    @MinAge(format = "yyyy-MM-dd", age = 18)
    private String birthDate;
    
    @Required
    @HttpsLink(message = "Profile URL must use HTTPS")
    private String profileUrl;
}

try {
    Validator.validOrFail(registrationForm);
} catch (InvalidEvaluationException e) {
    System.err.println(e.getKey() + ": " + e.getMessage());
}

Event Scheduler

Validator eventValidator = new Validator.Builder()
    .required("Event date is required")
    .date("yyyy-MM-dd", "Date must be in YYYY-MM-DD format")
    .expirationDate("yyyy-MM-dd", "Event date cannot be in the past")
    .build();

Validator timeValidator = new Validator.Builder()
    .required()
    .time24("Time must be in 24-hour format (HH:mm)")
    .build();

Server Configuration

public class ServerConfig {
    @Required
    @Ipv4(message = "Invalid server IP address")
    private String serverIp;
    
    @Ipv4
    private String gateway;
    
    @Ipv4
    private String dnsServer;
    
    @HttpsLink
    private String apiEndpoint;
}

See Also

Build docs developers (and LLMs) love