Articles on: Working with OpnForm

How to add a Regex condition to my input fields?

What is Regex?

Regex, short for regular expressions, is a sequence of characters that defines a search pattern. It is often used to validate input data by ensuring it follows a specific format or rules. For instance, regex can check if an email address contains the proper components, or if a phone number contains the correct number of digits.

Regex allows you to create precise validation rules for your form fields, ensuring data consistency and accuracy.

*

**How to Add Regex Validation in OpnForm**



Follow these steps to apply regex validation to a form field in OpnForm:

Go to the Field Settings

Open the settings of the specific field you want to validate.

Click on the Logic Tab

Navigate to the Logic tab to configure conditions for the field.

Add a Condition


Under the conditions section, select either Match or Does not match RegEx.

Enter your regex pattern in the provided input box.

*

**Sample Regex Patterns and Their Use Cases**



Email Validation:

Pattern: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$

Validates emails such as user@example.com.

Phone Number Validation:

Pattern: ^\\+?[0-9]{10,15}$

Ensures the input is a phone number with 10 to 15 digits, optionally starting with a "+".

Password Strength:

Pattern: ^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$

Ensures passwords are at least 8 characters long, with at least one letter and one number.

URL Validation:

Pattern: ^(https?:\\/\\/)?(www\\.)?[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}(/.*)?$

Validates URLs like https://example.com.

*

**Example Code for Regex Validation**



Here is an example of how regex might be applied in OpnForm:

Regex Pattern:

To validate a phone number:

^\\+?[0-9]{10,15}$


Steps in OpnForm:

Go to the field settings for the "Phone Number" field.
Click on the Logic tab.
Add a condition:

Select Match RegEx.

Enter the regex pattern above.

When users input data into this field, it will only be accepted if it matches the regex pattern, ensuring that only valid phone numbers are submitted.

*

"A sample regex that accepts only letters will trigger the display of an email input field."

Code: ^[A-Za-z]+$





**Tips for Using Regex**



Test Your Pattern: Use a regex tester (available online) to verify your pattern before applying it.
Keep It Simple: Overly complex patterns can confuse users and lead to incorrect rejections.
Provide User Feedback: If a user’s input does not match the regex, display a helpful error message explaining the expected format.

Updated on: 23/12/2024

Was this article helpful?

Share your feedback

Cancel

Thank you!