Get insights.
Unlock value.
- 14-day free trial
- Set up in minutes
- End-to-end encrypted
Form Input Validation Using Regex
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?
How do you use regex for form validation?
What are the two main types of form validation?
What is the HTML5 pattern attribute?
Why is form input validation important?
Can regex validate a cryptocurrency wallet address?
Are there limitations to using regex for form validation?
Get insights.
Unlock value.
- 14-day free trial
- Set up in minutes
- End-to-end encrypted