Comprehensive Guide to Validations in Power Platform: Best Practices for Controls

Comprehensive Guide to Validations in Power Platform: Best Practices for Controls

Validation is the foundation of integrity of data and user-friendly apps. You can use validations on controls in Power Platform to ensure that the data you are getting and processing is reliable. We are going to cover a broad variety of validation scenarios and how you can use them in Power Platform in the following section.


1. Age Validation

Scenario: Ensure that the user’s age is 18 or above.

Implementation:

  • Use a Date Picker control to capture the user’s date of birth.
  • Add a formula to validate the age:
If(DateDiff(DateValue(DateOfBirth.SelectedDate), Today(), TimeUnit.Years) < 18, Notify("You must be at least 18 years old.", NotificationType.Error))

2. Email Validation

Scenario: Ensure the entered email address is in the correct format.

Implementation:

  • Use a Text Input control to capture the email.
  • Validate the email using a regular expression:

If(!IsMatch(EmailInput.Text, “^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$”), Notify(“Please enter a valid email address.”, NotificationType.Error))


3. Weekend Date Validation

Scenario: Restrict the selection of weekend dates.

Implementation:

  • Use a Date Picker control.
  • Add a formula to validate the selected date:
If(Weekday(DatePicker.SelectedDate) in [1, 7], Notify("Weekends are not allowed.", NotificationType.Error))

4. Phone Number Validation

Scenario: Ensure the entered phone number follows a specific format (e.g., 10 digits).

Implementation:

  • Use a Text Input control.
  • Validate using a regular expression:
If(!IsMatch(PhoneNumber.Text, "^\d{10}$"), Notify("Please enter a valid 10-digit phone number.", NotificationType.Error))

5. Required Field Validation

Scenario: Ensure mandatory fields are not left empty.

Implementation:

  • Use the If function to check for blank values:
If(IsBlank(TextInput.Text), Notify("This field is required.", NotificationType.Error))

6. Numeric Range Validation

Scenario: Validate that a number falls within a specific range (e.g., 1 to 100).

Implementation:

  • Use a Text Input control and validate:
If(Value(NumericInput.Text) < 1 || Value(NumericInput.Text) > 100, Notify("Value must be between 1 and 100.", NotificationType.Error))

7. Prevent Duplicate Entries

Scenario: Prevent users from entering duplicate values.

Implementation:

  • Use a Gallery or Collection to check existing values:
If(LookUp(DataSource, FieldName = TextInput.Text, true), Notify("Duplicate entry detected.", NotificationType.Error))

8. Future Date Restriction

Scenario: Ensure a date field does not allow future dates.

Implementation:

  • Use a Date Picker control:
If(DatePicker.SelectedDate > Today(), Notify("Future dates are not allowed.", NotificationType.Error))

9. Special Character Restriction

Scenario: Prevent the use of special characters in a text field.

Implementation:

  • Use the IsMatch function:
If(IsMatch(TextInput.Text, "[!@#$%^&*(),.?\":{}|<>]"), Notify("Special characters are not allowed.", NotificationType.Error))

10. Password Complexity Validation

Scenario: Enforce a strong password policy (e.g., minimum 8 characters, includes letters, numbers, and special characters).

Implementation:

  • Use a Text Input control:

If(!IsMatch(PasswordInput.Text, "^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&#])[A-Za-z\d@$!%*?&#]{8,}$"), Notify("Password must be at least 8 characters and include letters, numbers, and special characters.", NotificationType.Error))

11. File Upload Validation

Scenario: Restrict file uploads to specific types (e.g., .pdf, .docx).

Implementation:

  • Use a File Attachment control:
If(!EndsWith(FileAttachment.FileName, ".pdf") && !EndsWith(FileAttachment.FileName, ".docx"), Notify("Only PDF or DOCX files are allowed.", NotificationType.Error))

12. Conditional Field Validation

Scenario: Validate a field only if another field meets a specific condition.

Implementation:

  • Use an If statement with multiple conditions:
If(Dropdown.Selected.Value = "Other" && IsBlank(TextInput.Text), Notify("Please specify details for 'Other'.", NotificationType.Error))

13. URL Validation

Scenario: Ensure a valid URL format.

Implementation:

  • Use the IsMatch function:
If(!IsMatch(UrlInput.Text, "^(https?:\\/\\/)?([\\w.-]+)\\.([a-z\\.]{2,6})([\\/\\w .-]*)*\\/?$"), Notify("Please enter a valid URL.", NotificationType.Error))

14. Decimal Validation

Scenario: Restrict input to only allow decimal numbers.

Implementation:

  • Use the IsMatch function:
If(!IsMatch(DecimalInput.Text, "^\\d+(\\.\\d{1,2})?$"), Notify("Please enter a valid decimal number.", NotificationType.Error))

15. Minimum Text Length Validation

Scenario: Ensure a text field contains a minimum number of characters.

Implementation:

  • Validate using the Len function:
If(Len(TextInput.Text) < 5, Notify("Text must be at least 5 characters long.", NotificationType.Error))

16. Maximum Text Length Validation

Scenario: Ensure a text field does not exceed a maximum number of characters.

Implementation:

  • Validate using the Len function:
If(Len(TextInput.Text) > 50, Notify("Text cannot exceed 50 characters.", NotificationType.Error))

17. Dropdown Selection Validation

Scenario: Ensure the user selects an option from a dropdown.

Implementation:

  • Use the IsBlank function:
If(IsBlank(Dropdown.Selected.Value), Notify("Please select an option.", NotificationType.Error))

18. Custom Date Range Validation

Scenario: Ensure the selected date falls within a specific range.

Implementation:

  • Validate using the If function:
If(DatePicker.SelectedDate < DateValue("01/01/2023") || DatePicker.SelectedDate > DateValue("12/31/2023"), Notify("Date must be within the year 2023.", NotificationType.Error))

19. Character Case Validation

Scenario: Ensure text is entered in uppercase.

Implementation:

  • Use the Upper function:
If(TextInput.Text <> Upper(TextInput.Text), Notify("Text must be in uppercase.", NotificationType.Error))

20. Multi-Select Validation

Scenario: Ensure at least one option is selected in a multi-select dropdown.

Implementation:

  • Validate using the CountRows function:
If(CountRows(MultiSelectDropdown.SelectedItems) = 0, Notify("Please select at least one option.", NotificationType.Error))

21. Cross-Field Dependency Validation

Scenario: Ensure a field is filled based on another field’s value.

Implementation:

  • Use If to check dependencies:
If(IsBlank(TextInput2.Text) && !IsBlank(TextInput1.Text), Notify("Field 2 is required when Field 1 is filled.", NotificationType.Error))

22. Alphanumeric Restriction

Scenario: Ensure text contains only alphanumeric characters.

Implementation:

  • Use the IsMatch function:
If(!IsMatch(TextInput.Text, "^[a-zA-Z0-9]+$"), Notify("Only alphanumeric characters are allowed.", NotificationType.Error))

23. Exact Length Validation

Scenario: Ensure input is exactly a specified length (e.g., 10 characters).

Implementation:

  • Use the Len function:
If(Len(TextInput.Text) <> 10, Notify("Input must be exactly 10 characters long.", NotificationType.Error))

24. Restricted Words Validation

Scenario: Prevent input of specific prohibited words.

Implementation:

  • Use IsMatch with a word list:
If(IsMatch(TextInput.Text, "(badword1|badword2)"), Notify("Prohibited words detected.", NotificationType.Error))

25. Minimum Items in Gallery Validation

Scenario: Ensure a gallery has a minimum number of selected items.

Implementation:

  • Use the CountRows function:
If(CountRows(Gallery.SelectedItems) < 3, Notify("Please select at least 3 items.", NotificationType.Error))

26. Overlapping Time Validation

Scenario: Ensure no overlap in time entries.

Implementation:

  • Use LookUp to check for overlaps:
If(!IsBlank(LookUp(DataSource, StartTime <= ThisItem.EndTime && EndTime >= ThisItem.StartTime)), Notify("Time entries overlap.", NotificationType.Error))

27. No Leading or Trailing Spaces

Scenario: Prevent leading or trailing spaces in text.

Implementation:

  • Use the Trim function:
If(TextInput.Text <> Trim(TextInput.Text), Notify("No leading or trailing spaces allowed.", NotificationType.Error))

28. Positive Number Validation

Scenario: Ensure input is a positive number.

Implementation:

  • Use the Value function:
If(Value(TextInput.Text) <= 0, Notify("Please enter a positive number.", NotificationType.Error))

29. Weekend and Holiday Restriction

Scenario: Restrict weekends and predefined holidays.

Implementation:

  • Use Weekday and a holiday list:
If(Weekday(DatePicker.SelectedDate) in [1, 7] || DatePicker.SelectedDate in HolidayList, Notify("Weekends and holidays are not allowed.", NotificationType.Error))

30. Max Number of Attachments

Scenario: Restrict the number of files attached.

Implementation:

  • Use CountRows:
If(CountRows(FileAttachmentControl.Attachments) > 5, Notify("A maximum of 5 files can be attached.", NotificationType.Error))

Conclusion

Validations on Power Platform are the assurance of data and a better user experience. You can apply multiple validations based on your application with PowerFX formulas and creative control settings. Accurate validation does not only protect your data but it also builds trust with the users.

What other checks are you missing? Comment below with any thoughts!