Form Input Validation Using Regex

Blocksurvey blog author
Written by Wilson Bright
Sep 24, 2023 · 5 mins read

When you collect data with BlockSurvey, you often need to check that thousands of email addresses or phone numbers from respondents are actually valid. Doing that by hand is slow and easy to get wrong.

Regular Expressions, or Regex, handle this kind of check. A Regex is a sequence of characters that forms a search pattern, used mainly to match patterns within strings.

This guide covers how Regex applies to data validation. You do not need a technical background to follow it. Whether you run a business or are just learning, the examples below show what Regex can do.

BlockSurvey includes a set of ready-made Regex expressions that validate a range of crypto addresses and many other question types supported by the platform. They are:

Regex for the Crypto World

Regular expressions let you validate the address formats used across different blockchain networks, which helps prevent errors and keeps blockchain address entries consistent.

  • If you are expecting a Bitcoin address input - ^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$
  • If you are expecting a Stacks address input- ^(SP|ST)[\da-km-zA-HJ-NP-Z]{38}$
  • If you are expecting an Ethereum address input or addresses from specific chains based on the Ethereum network - ^0x[a-fA-F0-9]{40}$
  • If you are expecting a Solana address input- - ^[1-9A-HJ-NP-Za-km-z]{44}$

These patterns match the address formats known today. Formats can change over time or differ between platforms, so check the official documentation and guidelines for a given blockchain to confirm the current, correct validation pattern for addresses on that network.

Short Text question type

This is the most basic question type in any data collection method, and it expects short text answers. Regex can handle a wide range of checks here, so the possibilities are broad. Here are a few common patterns you can try:

  • If you are expecting an answer about "n" characters long in lower case - ^[a-z]{1,n}$
  • If you are expecting an answer about "n" characters long in upper case - ^[A-Z]{1,n}$
  • If you want to restrict the input to certain words - ^(Apple|Orange|Banana)$
  • If you want to restrict the input to certain words . eg: This Regex will be valid for both "fear" or "fair" - ^f(ea|ai)r$
  • If you want "Name" as the input, then this Regex will check if its in the format "John Doe" - ^([A-Z]{1}[a-z]{1,}\s?)+$
  • If you want to restrict a character within a word by using the ? (quantifier) e.g. allow either color or colour as an input - ^colou?r$
  • If you want to restrict a character within a word by using the * (quantifier) e.g. allow either o! or oh! or ohh! or ohhh! and so on as an input - ^oh*!$
  • If you want to restrict an input to a non-digit character (e.g. a or c or ! or # or % etc.) - ^\D$
  • If you want to restrict an input to five non-digit character (e.g. aZcB!#% etc.) - ^\D{5 }

Long Text question type

Long text questions are another commonly used question type for data collection.

  • Make respondents adhere to a specified word limit by using the following Regex - ^(\S+\s*){1,n}$

If you have niche word patterns to match in a long text question, you can also use any of the Regex from the Short Text question type above.

Number question type

The number question type is used heavily in data collection. Numbers are everywhere, but not every range matters to the people collecting the data. Usually there is a specific range that gives better insights, and validation helps make sure the numbers you receive fall within it.

  • If you want to restrict an input to one digit in between 0 to 9 - ^\d$
  • If you want to restrict an input to five digits in between 0 to 9 - ^\d{5}$
  • If you want to restrict to a number 1-4 digits in length - ^\d{1,4}$
  • If you want to a range of number 0-999 - ^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$
  • If you want to a whole number or up to two decimal places - ^\d+(\.\d{1,2})?$
  • If you want to restrict an input to either to 12 or 345 - ^(12|345)$
  • If you want to restrict an input between 01 to 99 digits where input format of a single number (like 1 or 2) is not allowed - ^[01-99]{2}$
  • If you want to restrict the input to even numbers between 0 and 10: ^(0|2|4|6|8|10)$

Email question type

Email is a common field in many data collection methods, and validating each address matters for accuracy. Regex handles this by checking that the entered emails follow a standard format.

  • If you want a basic email validation like checking "[email protected]" - ^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$
  • If you want to restrict email to a specific domain like say "gmail" - ^\w+([\.-]?\w+)*@gmail\.com$
  • If you want to restrict email to multiple specific domains like say "gmail & yahoo" - ^\w+([\.-]?\w+)*@(gmail\.com|yahoo\.com)$

Website question type

You can validate entered URLs to check that they meet specific requirements.

  • If you want a basic URL validation to check if it follows the structure of a URL - ^(http|https)://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\\S*)?$
  • If you're expecting a URL with 'www' and 'https' only - ^https://www\.[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\\S*)?$
  • If you're expecting a URL from a specific domain, say "example.com" - ^https?://(www\.)?example\.com(/\\S*)?$
  • If you want to accept URLs from multiple specific domains, say "example.com" and "sample.com" - ^https?://(www\.)?(example|sample)\.com(/\\S*)?$

Regex for validation of social profile URL's

  • Regex for Facebook username - (?:https?:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*([\w\-\.]*)
  • Regex for Twitter URL - http(?:s)?:\/\/(?:www\.)?twitter\.com\/([a-zA-Z0-9_]+)
  • Regex for Reddit URL - ^http(?:s)?://(?:www\.)?(?:[\w-]+?\.)?reddit.com(/r/|/user/)?(?(1)([\w:]{2,21}))(/comments/)?(?(3)(\w{5,6})(?:/[\w%\\\\-]+)?)?(?(4)/(\w{7}))?/?(\?)?(?(6)(\S+))?(\#)?(?(8)(\S+))?$

Phone Number question type

A phone number is a piece of information that data collectors often request from respondents.

  • If you want to check if a phone number entered is a valid US phone number - ^\+1\(?([2-9][0-8][0-9])\)?[-.●]?([2-9][0-9]{2})[-.●]?([0-9]{4})$
  • If you want to check if a phone number entered is a valid Indian phone number - ^\+91[-\s]?\d{5}[-\s]?\d{5}$

Date question type

The date question type is a core part of many data collection methods, letting you capture specific dates or ranges. It is useful for event registrations, surveys, and booking systems. With date inputs, you can gather birthdays, appointment schedules, project timelines, and more.

  • If you want to check if a date is from January and your date is in the format "YYYY-MM-DD" - ^\d{4}-01-\d{2}$
  • If you want to check if a date is from January and your date is in the format "MM-DD-YYYY" - ^01-\d{2}-\d{4}$
  • If you want to check if a date is from January and your date is in the format "DD-MM-YYYY" - ^\d{2}-01-\d{4}$

Country question type

A predefined list of countries lets respondents report their location accurately and consistently. This information supports demographic analysis, market segmentation, and region-specific experiences.

  • If you want to restrict the answer to be "United States" or "Canada" - ^(United States|Canada)$
  • If you want to make sure the answer starts with "A" (for countries like Australia, Argentina, Austria, etc.) - ^A\w+
  • If you want to ensure the answer is exactly "United Kingdom" - ^United Kingdom$
  • If you want to accept only countries starting with "New" (like New Zealand) - ^New \w+

Conclusion

Regex is a flexible, efficient way to handle many data validation scenarios, from simple text inputs to patterns for email addresses, phone numbers, and dates. With regular expressions, you can set validation rules that enforce specific patterns and formats, which keeps the data you collect accurate and consistent.

Explore related BlockSurvey features: email verification and file upload.

Form Input Validation Using Regex FAQ

How does regex validation work?

Regex validation compares a respondent's input against a defined pattern before the form accepts it. If the input matches the pattern the field is marked valid; if it doesn't, the form shows an error and blocks submission. For example, the pattern ^\d{5}$ only accepts a 5-digit ZIP code.

How do you use regex for form validation?

You write a pattern describing the exact structure a valid answer must have, then attach that pattern to the input field so it checks the response as the respondent types or submits. In BlockSurvey this is done by adding a custom regex rule to a Short Text, Long Text, Number, Email, Website, Phone Number, Date, or Country question, and any entry that doesn't match the pattern is rejected with a prompt to correct it.

What are the two main types of form validation?

The two main types are client-side and server-side validation. Client-side validation checks input in the browser, using regex patterns or the HTML5 pattern attribute, and gives instant feedback before submission, while server-side validation re-checks the same data after it reaches the server to catch anything tampered with or bypassed.

What is the HTML5 pattern attribute?

The HTML5 pattern attribute lets a developer attach a regular expression directly to an input element so the browser validates it natively without extra JavaScript. If a respondent's entry doesn't match the regex in the pattern attribute, the browser blocks submission and shows a built-in validation message. Survey tools like BlockSurvey apply the same regex-matching logic through form settings instead of raw HTML markup.

Why is form input validation important?

Form input validation keeps malformed or malicious data out of survey responses and gives respondents instant feedback when an answer doesn't fit the expected format. This reduces data cleanup after collection and ensures fields like email, phone number, and date are usable as soon as they're submitted.

Can regex validate a cryptocurrency wallet address?

Yes. A regex pattern can check that a wallet address matches the character set and length a given blockchain uses, for example ^0x[a-fA-F0-9]{40}$ for Ethereum or ^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$ for Bitcoin. BlockSurvey provides curated patterns for Bitcoin, Ethereum, Stacks (STX), and Solana (SOL) wallet addresses so survey creators don't have to write them from scratch.

Are there limitations to using regex for form validation?

Regex can only confirm that input matches a format; it cannot verify that the underlying data is true or that an account, such as a wallet address or email, actually exists. Complex patterns can also be difficult to write and read, so some validation rules are better handled with a different method than a single regex pattern.

Like what you see? Share with a friend.


blog author description

Wilson Bright

Wilson Bright is the co-founder of BlockSurvey, an AI-native, privacy-first survey platform designed to help Institutional Researchers uncover deeper, more actionable insights. He believes the future of Institutional Research lies in combining ethical data collection with intelligent automation to make evidence-based decisions faster, fairer, and more transparent.

SHARE

Explore more
Scripts are blocked. This site won’t work properly. If you’re using Brave, click the Shields icon and turn off Block scripts. Otherwise disable your ad blocker for this site.