Form Input Validation Using Regex

Blocksurvey blog author
Written by Sam Joseph
Sep 24, 2023 · 5 mins read

Imagine, while collecting data using BlockSurvey, you're also tasked with checking the validity of thousands of email addresses or phone numbers which the respondents answer - daunting, right?

Enter Regular Expressions, or Regex, a secret weapon for handling such tasks with ease. Regex is a sequence of characters that forms a search pattern, primarily used for pattern matching with strings.

In this blog, we'll explore the fascinating world of Regex, focusing on its role in data validation. You don't need to be a tech whiz to understand - we'll break it down in a friendly, accessible manner. So, whether you're a business owner, a curious learner, join us on this illuminating journey into the power of Regex!

BlockSurvey offers a comprehensive collection of curated Regex expressions specifically designed to validate a wide range of crypto addresses and various other question types supported by the platform. They are,

Regex for the Crypto World

Regular expressions are versatile tools that can be effectively utilized for validating the specific patterns of addresses across different blockchain networks, thus helping prevent errors and maintaining consistency when handling blockchain addresses.

  • 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}$

Please note that regex patterns for validating blockchain addresses are based on the current known formats and standards, but these formats can evolve over time or differ between blockchain platforms, so it is essential to refer to the official documentation and guidelines provided by the specific blockchain platform to ensure accurate and up-to-date validation patterns for addresses on that particular blockchain.

Short Text question type

The most basic of question type you would find in any data collection method expecting the answers to be short texts. If you are creative enough, the possibilities of Regex being applied in these cases is endless. Here are examples of a few common Regex you could 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 type questions are another set of the commonly used question types for data collection.

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

You can use any of the Regex mentioned in Short Text question type if you have got very niche word patterns to match in your Long Text question type.

Number question type

The number question type finds tremendous usage in the field of data collection. There are numbers everywhere, but not all range of numbers are important to the data collectors. Often there exists a range of numbers which the data collectors will be looking for to provide better insights. Therefore validations go a long way in helping the data collectors get those numbers in the expected range.

  • 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 it's crucial to validate these addresses for accuracy. The power of Regular Expressions (Regex) comes into play here, ensuring the entered emails adhere to 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

Validations can be performed to check if the URL's entered 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 an important information which data collectors often request from the 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 fundamental component in various data collection methods, enabling the capture of specific dates or ranges. It plays a crucial role in scenarios such as event registrations, surveys, or booking systems. With date inputs, you can gather information on birthdays, appointment schedules, project timelines, and much 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

By offering a predefined list of countries, respondents can provide their location accurately and consistently. This information aids in demographic analysis, market segmentation, and tailoring experiences to specific regions

  • 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 provides a flexible and efficient way to handle a wide range of data validation scenarios, from simple text inputs to complex patterns like email addresses, phone numbers, dates, and more. By leveraging regular expressions, we can implement robust validation rules that enforce specific patterns and formats, allowing us to validate inputs effectively for ensuring data accuracy and integrity.

Form Input Validation Using Regex FAQ

What is form input validation using Regex?

Form input validation using Regex refers to the process of validating user input in a web form using regular expressions (Regex) to ensure that the entered data meets specific criteria or patterns.

How does form input validation using Regex work?

Regex patterns are applied to the user input, checking if it matches the desired format or criteria defined by the developer. If the input matches the pattern, it is considered valid; otherwise, an error message is displayed.

Why is form input validation important?

Form input validation is crucial for ensuring data integrity and security. It helps prevent malicious or incorrect data from being submitted, improves user experience by providing instant feedback, and ensures accurate processing of form submissions.

Can Regex be used for all types of form input validation?

Regex can be used for most types of form input validation. It is particularly useful for validating text-based input fields, such as email addresses, phone numbers, dates, URLs, and more.

Are there any limitations or challenges when using Regex for form input validation?

Regex can be complex to write and understand, leading to potential errors or inefficient patterns. Additionally, some input validation rules might be better suited for other validation methods. It is essential to carefully consider the trade-offs and choose the most appropriate approach. You can use chatGPT and other language models to generate your Regex based on your requirements.

Like what you see? Share with a friend.


blog author description

Sam Joseph

Sam Joseph heads the Design Team at BlockSurvey and is responsible for making BlockSurvey a great user friendly tool. He enjoys playing the keyboard and the sport of badminton.

SHARE

Explore more