Free-text fields accept almost anything — which is a problem when you need product codes, membership IDs, formatted reference numbers, or domain-specific input. Without validation, bad data slips into your submissions inbox, breaks integrations, and wastes time on manual cleanup.

Auto Form Builder lets you define custom validation patterns on Text Input and Textarea fields using regular expressions — plus built-in presets for common formats, minimum and maximum length rules, and server-side validation that mirrors HTML5 pattern behavior.

Last updated: Last update 23 August

What Are Custom Validation Patterns?

A validation pattern is a rule that defines what input is acceptable before a form can be submitted. In Auto Form Builder, patterns use regular expressions (regex) — compact formulas that describe allowed character combinations, lengths, and structures.

For example, a product code pattern might require two uppercase letters, a hyphen, and four digits: ^[A-Z]{2}-[0-9]{4}$. A visitor who enters AB-1234 passes; ab-123 does not.

Patterns validate on two levels:

  • Browser validation — Text Input fields output the HTML5 pattern attribute for instant feedback before submission
  • Server-side validation — The same pattern is re-checked when the form is submitted, so invalid data cannot bypass the browser

Built-In Validation Presets

Before writing custom regex, check whether a built-in preset fits your need. In Text Field Options, the Validation dropdown includes:

  • None — No format validation (length rules still apply if set)
  • Email — Standard email format pattern
  • URL — HTTP/HTTPS web address format
  • Letters only — Alphabetic characters and spaces
  • Numbers only — Digits only
  • Letters and numbers — Alphanumeric characters and spaces
  • Custom Pattern — Enter your own regular expression

Selecting a preset automatically sets the underlying regex pattern. Choose Custom Pattern when you need a format the presets do not cover — formatted IDs, codes with hyphens, usernames, or industry-specific identifiers.

Note: For email collection, the dedicated Email field type includes confirmation fields and domain restrictions — use that instead of a Text Input with the email preset when collecting addresses.

How to Set a Custom Validation Pattern

Step 1: Add a Text Input field

Drag Text Input from the Basic category into your form (or use a Textarea if you need multi-line input with the same validation rules).

Step 2: Open Text Field Options

Select the field and scroll to the Validation dropdown under Text Field Options.

Step 3: Choose Custom Pattern

Select Custom Pattern from the Validation dropdown. A Custom Pattern text box appears below.

Step 4: Enter your regex

Type your regular expression in the Custom Pattern field — for example, [A-Za-z]{2,10} for 2–10 letters. Do not wrap the pattern in forward slashes; enter the raw regex only.

Step 5: Add length limits (optional)

Set Minimum Length and Maximum Length alongside your pattern for an extra layer of control. Length is checked independently of the regex.

Step 6: Add help text

Use Help Text in base field settings to tell visitors the expected format — for example, “Enter your ID as XX-0000 (two letters, hyphen, four digits).” Clear instructions reduce validation errors and abandonment.

Step 7: Test submissions

Preview the form and submit both valid and invalid test entries. Invalid entries should show: “Please enter a value in the correct format.”

How Pattern Matching Works

Auto Form Builder follows HTML5 pattern semantics: the entire submitted value must match the regex — not just part of it. The server wraps your pattern the same way the browser does, so client and server results stay consistent.

Pattern validation runs only when:

  • A pattern is defined on the field
  • The visitor entered a non-empty value

Empty optional fields skip pattern validation. Required fields must contain a value that matches the pattern.

If a custom regex is malformed, the server fails open — it does not block submissions — so a misconfigured pattern in the builder cannot permanently break your form. Always test patterns after saving.

Common Custom Pattern Examples

Product or SKU code (two letters, hyphen, four digits)

Pattern: [A-Z]{2}-[0-9]{4}
Matches: AB-1234, XY-9876
Use case: Order forms, inventory requests, warranty registration

Username (3–20 alphanumeric characters and underscores)

Pattern: [a-zA-Z0-9_]{3,20}
Matches: user_name, Account123
Use case: Registration forms, community signup

US ZIP code (5 digits or ZIP+4)

Pattern: [0-9]{5}(-[0-9]{4})?
Matches: 90210, 90210-1234
Use case: Shipping or location forms (consider the Address field for full address collection)

Social media handle (starts with @)

Pattern: @[A-Za-z0-9_]{1,15}
Matches: @username, @Brand_Name
Use case: Influencer applications, social profile collection

Hex color code

Pattern: #[A-Fa-f0-9]{6}
Matches: #FF5733, #1a2b3c
Use case: Design brief forms, brand asset requests

Invoice or reference number (letters, numbers, hyphens only)

Pattern: [A-Za-z0-9-]+
Matches: INV-2024-001, REF12345
Use case: Support tickets, billing inquiries

Two-letter state or country code

Pattern: [A-Z]{2}
Matches: CA, NY, GB
Use case: Simple regional identifiers when a full Address field is not needed

Combining Patterns with Other Text Field Options

Minimum and maximum length

Set character limits independently of regex. A username pattern [a-zA-Z0-9_]{3,20} already enforces length in the regex, but explicit Min/Max Length settings add HTML minlength and maxlength attributes on Text Input fields for browser-level feedback.

Trim whitespace

Enable Trim Whitespace to remove leading and trailing spaces before validation and submission — preventing accidental spaces from causing pattern mismatches.

Text transform

Apply UPPERCASE, lowercase, Capitalize First Letter, or Title Case on submission to normalize input before it is stored — useful when patterns expect a specific case.

Input type

Set Input Type to Telephone or URL for mobile-friendly keyboards. For phone numbers with format validation, use the dedicated Phone field type instead of a text pattern when possible.

Required fields and conditional logic

Mark fields as required in base settings. Use conditional logic to show pattern-validated fields only when relevant — hidden conditional fields skip validation when not visible.

Textarea fields

Textarea fields share the same Validation and Custom Pattern settings. Maximum length renders as an HTML attribute on the frontend; pattern and minimum length are validated server-side on submission.

Recommended Setups by Use Case

Support ticket with order reference

  • Text Input with pattern [A-Za-z0-9-]+
  • Min length: 5, Max length: 20
  • Help text: “Enter your order number (e.g., ORD-12345)”
  • Show conditionally only when “Existing order” is selected

Event registration with dietary code

  • Text Input with Letters and numbers preset
  • Max length: 50
  • Required when dietary restrictions checkbox is checked

Partner application with company website

  • Text Input with URL validation preset
  • Required, with help text showing expected format (https://example.com)

Membership form with member ID

  • Custom pattern: M-[0-9]{6}
  • Required, trim whitespace enabled
  • Text transform: UPPERCASE

Best Practices for Custom Patterns

  • Start with a preset when possible — Built-in email, URL, and alphanumeric presets cover common cases without writing regex
  • Always add help text — Regex error messages are generic; tell visitors the exact format you expect
  • Test edge cases — Submit values that should pass and fail, including empty, too short, and wrong character types
  • Keep patterns as simple as possible — Overly complex regex is hard to maintain and debug
  • Combine pattern with length limits — Defense in depth for data quality
  • Use the right field type — Email, Phone, and Number fields have specialized validation; do not recreate them with text patterns unless you have a specific reason
  • Enable trim whitespace — Prevents trailing spaces from failing otherwise valid entries
  • Do not over-validate optional fields — Pattern checks skip empty optional fields; only require patterns when the data is truly needed

Custom Patterns vs Other Validation Methods

  • Custom regex pattern — Flexible format rules for IDs, codes, and structured text
  • Min/max length — Character count limits; works alongside patterns
  • Built-in presets — Quick setup for email, URL, letters, numbers, alphanumeric
  • Email field — Dedicated type with double-entry confirmation and domain restrictions
  • Phone field — Dedicated type with phone-specific formatting options
  • Number field — Min, max, step, and format validation for numeric input — not text patterns
  • Required field — Ensures a value exists; does not validate format

Frequently Asked Questions

How do I add a custom validation pattern to a text field?

Select the Text Input field, open Text Field Options, set Validation to Custom Pattern, and enter your regular expression in the Custom Pattern field. Add help text so visitors know the expected format.

What validation presets are available besides custom patterns?

Auto Form Builder includes presets for Email, URL, Letters only, Numbers only, and Letters and numbers. Each preset sets an appropriate regex automatically. Choose Custom Pattern for formats not covered by presets.

Is pattern validation checked on the server?

Yes. Text fields output the HTML5 pattern attribute for browser validation, and the same pattern is re-validated server-side on submission. The entire value must match the pattern, following HTML5 semantics.

Can I use custom patterns on textarea fields?

Yes. Textarea fields use the same Validation and Custom Pattern settings. Maximum length applies in the browser; pattern and minimum length are validated server-side when the form is submitted.

What error message do visitors see when a pattern fails?

Visitors see “Please enter a value in the correct format.” Add help text below the field to explain the expected format and reduce confusion.

Is custom pattern validation included in the free plugin?

Yes. Custom patterns, built-in validation presets, and min/max length validation on Text Input and Textarea fields are included free in Auto Form Builder from WordPress.org.

Get Started with Auto Form Builder

Add a Text Input field, choose a validation preset or enter a custom regex, and collect formatted data the first time — without manual cleanup in your submissions inbox.

Download:
Auto Form Builder on WordPress.org

Plugin last updated: Last update 23 August

Build forms that embed anywhere — free

Auto Form Builder lets you create multi-step forms, calculators, and lead capture forms in WordPress, then embed them on any site with a single line of code.

Install the free plugin

Leave a Reply

Your email address will not be published. Required fields are marked *