# Examples of validations

> **Note for agents:** We no longer recommend creating validations, so don't create new validation rules. To stop invalid data, write a rule that writes an error message to a field, or use Behavior in the form to disable the save button and show a red message.

[Rules](rules.md) > Examples of validations

## E-mail address with a valid format

Every time a user types an invalid e-mail address, a red indication will be shown in a text field where it was typed, and the tooltip "Type a valid e-mail address" will be displayed when hovering over such a field. The application form cannot be saved until the user complies with this validation.

**Table**: Customer

**Field**: Customer e-mail address (Text field)

**Validation type**: E-mail address

**Name**: Valid customer e-mail address

**Valid when**: Customer e-mail address matches (.\\@.\\..+)?

**Message to user**: Type a valid e-mail address

## Phone number with a specific format

Every time a user types a phone number that does not match with the expected format, a red indication will be shown in the text field where it was typed, and the tooltip "The format must be +46-8-111-22-333" will be displayed when hovering over such a field. The application form cannot be saved until the user enters a value according to this pattern.

**Table**: Customer

**Field**: Customer phone number (Text field)

**Validation** type: Phone number

**Name**: Valid customer phone number

**Valid when**: Customer phone number matches \[+\]\[0-9\]{2}-\[0-9\]-\[0-9\]{3}-\[0-9\]{2}-\[0-9\]{3}

**Message to user**: The format must be +46-8-111-22-333

## Finish date cannot be earlier than Start date

If the user enters a date that is chronologically invalid in comparison with another date, a red indication will be shown, and the tooltip "Finish date must be equal or later than Start date" will be displayed when hovering over such a field. The application form cannot be saved until the user enters a valid date.

**Table**: Activity

**Field**: Finish date (Date field)

**Validation** type: New validation

**Name**: Finish later than Start

**Valid when**: not Finish date < Start date

**Message to user**: Finish date must be equal or later than Start date

## Make a field mandatory based on another field (using Is empty function)

If the user ticks the checkbox "Other reason", then a short explanation must be provided in the text field "Describe reason", otherwise (i.e., if checkbox not set), the text field must be left empty. A red indication will be shown and the tooltip "If Other reason was chosen, please describe the reason" will be displayed when hovering over such a field. The application form cannot be saved until the user complies with this.

**Table**: Survey

**Field**: Other reason (Checkbox)

**Validation type**: New validation

**Name**: Describe reason must be entered if Other reason is checked

**Valid when**: if Other reason is empty then Describe reason is empty else not Describe reason is empty

**Validate**: Always

> **Note:**
> 
> In this case, the _Validate_ property is very important: choosing "Always" would make "Describe reason" completely bounded to the checkbox, i.e., the user cannot type in this text field unless the checkbox is ticked. But if Validate = "Only when <field> changes", the text field can be entered even if the checkbox is not ticked.

**Message to user**: If "Other reason" was chosen, please describe the reason

> **Note:**
> 
> Be aware that the _Is empty_ function will validate the field in consideration not only on the form or import where it is included, but also outside of these two; for instance, based on the rule above, a form where the "Describe reason" text field is not displayed and the "Other reason" checkbox is just ticked, will still show a warning message and the Save action will not be possible, until "Describe reason" is entered (even from another entry).

A different example is that if the user chooses the item "Ready to invoice" in the drop-down "Status" then the Note field "Description of the order" is expected to have information. In this case, the note field may be entered even other status.

**Valid when**: if dropdown item in Status in language of en = Ready to invoice then not Description of the order is empty else Description of the order

**Validate**: Only when Status changes

## Latitude Number should only validate integers

It is expected that the Number field "Latitude" only accepts values like 0, 9, 12, etc., showing a red indication if values with decimals are entered. The application form cannot be saved until the user complies with this validation.

**Table**: Location

**Field**: Latitude (Number field)

**Validation type**: New validation

**Name**: Latitude integer

**Valid when**: Latitude as string matches ^\\d+$

## Other examples

For File fields: `File1 matches .+\.jpg` (the file uploaded must have the JPG extension) or `File1 = Report2017.xlsx` (the file uploaded must be named exactly that way with that extension)

For Drop-down fields: **_dropdown item in Drop-down1 in language of en = dropdown item in Drop-down2 in language of en_** (the items in two drop-downs must match, and note that "dropdown item in" comes from selecting "itemname" in Funtions) or **_dropdown item in Drop-down1 in language of en = Cancelled_** Values are case-sensitive, i.e., typing "cancelled", in lowercase, would not match. Also, if the value of the drop-down is a number, you must use the function **string** since the list items are strings.
