Skip to main content

validateDate()

const validateDate: (value, fieldName, minAge?, maxAge?) => string;

Validates a date field with age restrictions

Parameters

ParameterTypeDescription
valuestringThe date to validate in MM/DD/YY format
fieldNamestringThe name of the field for error messages
minAge?numberThe minimum allowed age (default: 13)
maxAge?numberThe maximum allowed age (default: 120)

Returns

string

Empty string if valid, first error message encountered if invalid

Note

Checks: required, valid MM/DD/YY format, age within range

Example

validateDate("", "Date of birth") // Returns: "Date of birth is required"
validateDate("13/01/23", "Date of birth") // Returns: "Please enter a valid date in MM/DD/YY format"
validateDate("01/01/15", "Date of birth", 18, 100) // Returns: "You must be at least 18 years old"