RegEx Validation
A RegEx, or Regular Expression, is a sequence of characters that forms a text pattern. RegEx can be used to check if a string contains the specified search pattern.
For example, if you want to collect US phone numbers and want to ensure that the respondents enter valid US phone numbers, you can apply a RegEx pattern to the question. By doing so, all the respondents will have to enter the telephone numbers matching to the US telephone number format - (xxx) xxx-xxxx. If the phone number fails to match that pattern, an error message will display indicating that it must match the appropriate format.
How to add RegEx validation?
RegEx pattern validation can be added to text input type questions.
- To add validation, switch on the toggle for validation on the text input type question.
- By default, Forced Validation is selected and type is Default.
- Click on Default and select the RegEx option.
- Now click on RegEx Pattern
- On the popup, you can select any of the predefined patterns.
- Or, click on Custom Pattern to add your own validation pattern.
- In this example, we want employees to enter their employee ID in the format: ABC1234-XY
- First 3 characters are upper case alphabets followed by four numbers followed by a hyphen followed by 2 upper case alphabets.
- Enter the RegEx custom pattern and add your validation message and Save
How does RegEx validation work while answering a survey?
- In this example, we want respondents to enter their employee id in the format: ABC1234-XY
- On the survey, if the pattern matches, response will be submitted and they can move ahead in the survey.
- If pattern doesn't match, error will be displayed asking respondents to enter the correct pattern.
Pre-defined RegEx patterns in SurveyAnalytics:
Currency and Numbers:
- A number 1-4 digits in length - ^\d{1,4}$
- Range of number 0-999 - ^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$
- A whole number or up to two decimal places - ^\d+(\.\d{1,2})?$
- A whole number percentage with '%' sign - ^\d{1,3}\%$
- Currency (Dollar and cents with commas) - ^$?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,} (\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$
- Currency (Euros with periods and comma) - ^\€?([1-9]{1}[0-9]{0,2}(\.[0-9]{3})*(\,[0-9]{0,2})?|[1-9]{1}[0-9]{0,} (\,[0-9]{0,2})?|0(\,[0-9]{0,2})?|(\,[0-9]{1,2})?)$
- Currency (French with spaces and comma) - ^\€?([1-9]{1}[0-9]{0,2}(\s[0-9]{3})*(\,[0-9]{0,2})?|[1-9]{1}[0-9]{0,} (\,[0-9]{0,2})?|0(\,[0-9]{0,2})?|(\,[0-9]{1,2})?)$
Dates and Times:
- Date in mm/dd/yyyy - ^(0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/](19|20)\d\d$
- Date in dd/mm/yyyy - ^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[012])[\/](19|20)\d\d$
- Date in yyyy/mm/dd - ^(19|20)\d\d[\/](0?[1-9]|1[012])[\/](0?[1-9]|[12][0-9]|3[01])$
- Date in mm/dd/yyyy or m/d/yyyy - ^(0?[1-9]|1[012])[\/](0?[1-9]|[12][0-9]|3[01])[\/](19|20)\d\d$
- Three character Month - (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept|Oct|Nov|Dec)$
- Time Format in HH:MM am/pm - ^([1-9]|1[012]):(0[0-9]|[1-5][0-9])\s?(am|AM|pm|PM)$
Contact Info and Demographics
- Email Address Format - ^([a-zA-Z0-9_\-\.]+)*(\+[a-z0-9-]+)?@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
- US Zip Code 5 or 9 digit (with dash) - ^\d{5}([\-]\d{4}){0,1}$
- US Phone Number - ^((\(\d{3}\) ?)|(\d{3}[-\s]))?\d{3}[-\s]\d{4}$
- UK Zip Codes - ^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$
- UK Phone Number - ^\s*\(?((\+0?44)?\)?[ \-]?(\(0\))|0)((20[7,8]{1}\)?[ \-]?[1-9]{1}[0-9]{2}[ \-]?[0-9]{4})|([1-8]{1}[0-9]{3}\)?[ \-]?[1-9]{1}[0-9]{2}[ \-]?[0-9]{3}))\s*$
- Height in Feet and Inches e.g. 6'3" - ^(?!$|.*\'[^"]+$)(?:([0-9]+)\')?(?:([0-9]+)"?)?$
Thank you for submitting feedback.