Back

validatesFormatOf()

Validates that the value of the specified property is formatted correctly by matching it against a regular expression using the regEx argument and / or against a built-in CFML validation type using the type argument (creditcard, date, email, etc.).

Name Type Required Default Description
properties string No Name of property or list of property names to validate against (can also be called with the property argument).
regEx string No Regular expression to verify against.
type string No One of the following types to verify against: creditcard, date, email, eurodate, guid, social_security_number, ssn, telephone, time, URL, USdate, UUID, variableName, zipcode (will be passed through to your CFML engine's IsValid() function).
message string No [property] is invalid Supply a custom error message here to override the built-in one.
when string No onSave Pass in onCreate or onUpdate to limit when this validation occurs (by default validation will occur on both create and update, i.e. onSave).
allowBlank boolean No false If set to true, validation will be skipped if the property value is an empty string or doesn't exist at all. This is useful if you only want to run this validation after it passes the validatesPresenceOf test, thus avoiding duplicate error messages if it doesn't.
condition string No String expression to be evaluated that decides if validation will be run (if the expression returns true validation will run).
unless string No String expression to be evaluated that decides if validation will be run (if the expression returns false validation will run).
1. Validate that a credit card number is correct
validatesFormatOf(property="cc", type="creditcard");

2. Validate that a US zipcode matches 5 or 9 digit format
validatesFormatOf(property="zipcode", type="zipcode");

3. Ensure that an email ends with `.se` when IP check returns true and today is not Sunday
validatesFormatOf(
    property="email",
    regEx="^.*@.*\.se$",
    condition="ipCheck()",
    unless="DayOfWeek() eq 1",
    message="Sorry, you must have a Swedish email address to use this website."
);

4. Validate that a username contains only letters, numbers, or underscores
validatesFormatOf(
    property="username",
    regEx="^[a-zA-Z0-9_]+$",
    message="Username can only contain letters, numbers, and underscores."
);

5. Validate multiple properties at once using built-in CFML types
validatesFormatOf(
    properties="phone,email",
    type="telephone,email",
    allowBlank=true
);

6. Validate only when updating an existing object
validatesFormatOf(
    property="ssn",
    type="social_security_number",
    when="onUpdate",
    message="Invalid SSN format for updating records."
);
Copied!