Advanced Validation with Regular Expressions

Learn how to use regular expressions (regex) to create powerful, flexible validation rules for your fields.

What You'll Learn

This guide introduces you to using regular expressions within validation rules. You'll discover how to create validation patterns that check for specific formatting, character combinations, and other detailed criteria that standard validation options can't handle.

Regular Expressions Overview

Regular expressions (regex) are special text strings that define search patterns. In validation rules, they allow you to specify exactly what format data must follow before it can be saved.

With regex, you can create validation rules for:

  • Specific formats like product IDs
  • Complex password requirements
  • Custom text patterns unique to your organization
  • Advanced character and length restrictions

Using Regex in Validation Rules

To implement a regex validation:

  1. Add a validation rule to your text field
  2. Select "matches regular expression" as the condition
  3. Enter your regex pattern in the value field
  4. Provide a clear error message explaining the required format

Common Regex Examples

Here are some useful regex patterns for common validation scenarios:

Numeric Patterns

  • Exactly 8 digits: ^[0-9]{8}$
  • Between 5-10 digits: ^[0-9]{5,10}$
  • Decimal number with 2 decimal places: ^\d+\.\d{2}$

Text Patterns

  • Uppercase letters only: ^[A-Z]+$
  • Exactly 3 uppercase letters: ^[A-Z]{3}$
  • Letters and spaces only: ^[A-Za-z ]+$

Format Validation

  • US Phone Number: ^\d{3}-\d{3}-\d{4}$
  • North American Postal Code: ^[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d$
  • US Zip Code: ^\d{5}(-\d{4})?$

Password Requirements

  • At least 8 characters with one uppercase, one lowercase, one number:
    ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
  • Between 8-20 characters with special characters:
    ^[A-Za-z\d@$!%*?&]{8,20}$

Testing Your Regex

Before implementing regex in your application, it's important to test it thoroughly:

  1. Use online tools like Regex101 to develop and test your patterns
    • Make sure to select the ECMAScript/JavaScript flavor
  2. Test with valid examples that should pass
  3. Test with invalid examples that should fail
  4. Check edge cases to ensure your pattern is neither too strict nor too lenient

Tip: Always test your regex with various examples, including edge cases, to ensure it works as expected.

Troubleshooting Regex

If you encounter issues with your regex validation:

  • Spinning Form Issue: If a form gets stuck spinning with no error message, there might be an error in your regex. Test your pattern using Regex101.
  • Too Restrictive: If valid entries are being rejected, your pattern may be too strict.
  • Too Permissive: If invalid entries are being accepted, your pattern may have gaps.
  • Performance Issues: Very complex regex patterns can impact performance. Keep patterns as simple as possible while meeting your requirements.

Limitations and Notes

  • Knack does not provide direct support for creating or troubleshooting regular expressions
  • Regular expressions involve code-based solutions and their full compatibility with Knack is not guaranteed
  • Validation rules using regex are not case-sensitive by default (use (?-i) to make them case-sensitive)
  • Regex validation applies only to form submissions, manual record additions, and inline edits—not to imports or batch updates

Note: While Knack cannot provide support for creating or troubleshooting regular expressions, many online resources can help you learn and implement regex effectively.