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.).
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!