By combining regular (regex) expressions in your JSONata expressions in , you can create complex patterns to match specific strings of text. Regex expressions are a sequence of characters that define a search pattern. It is powerful when used in text processing to find, replace, or validate strings of text. Some common uses of regex include:
Validating email addresses or phone numbers
Extracting specific information from a text file or document
Reformatting data to match a specific pattern or structure
Examples and code snippets
The JSONata + regex examples below create validation for text-fields in a form.
Expected results: Between 9 and 13 numbers with no spaces, can include a symbol for dialing code, e.g., +271234556789.
Expected result: Typically 13-16 digits, with spaces or dashes optional, and includes checks for Visa, MasterCard, American Express, and Discover. E.g. 1111-1111-1111-1111 or 1111 1111 1111 1111.
1-type: component.text-field
2instanceId: zip
3options:4label: ZIP/ Postal code
5errorText: =$contains(@ctx.components.zip.state.value, /^\d{5}(-\d{4})?$/) ? '' :'not a ZIP/ Postal code'6
Expected result: XXX-XX-XXXX
Valid Social Security number
Invalid Social Security number
YAML
1-type: component.text-field
2instanceId: social-security-number
3options:4label: Social security number
5errorText: =$contains(@ctx.components.social-security-number.state.value, /^\d{3}-\d{2}-\d{4}$/) ? '' :'not a social security number'6
Expected result: AA123456C
Valid National Insurance number
Invalid National Insurance number
YAML
1-type: component.text-field
2instanceId: national-insurance-number
3options:4label: National Insurance Number
5errorText: =$contains(@ctx.components.national-insurance-number.state.value, /^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\d{2}(?:\s*\d{2}){2}\s*[A-D]$/) ? '' :'not a national-insurance-number'6
Expected result: DD/MM/YYYY, e.g. 23/07/2024.
Valid date format
Invalid date format
YAML
1-type: component.text-field
2instanceId: us-date
3options:4label: Date - DD/MM/YYYY
5errorText: =$contains(@ctx.components.us-date.state.value, /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/(19|20)\d{2}$/) ? '' :'not a valid date (DD/MM/YYYY)'6
Expected result: MM/DD/YYYY, e.g. 03/28/2023.
Valid date format
Invalid date format
YAML
1-type: component.text-field
2instanceId: date
3options:4label: Date - MM/DD/YYYY
5errorText: =$contains(@ctx.components.date.state.value, /^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/(19|20)\d{2}$/) ? '' :'not a valid date (MM/DD/YYYY)'6
Expected result: DD Month YYYY, e.g. 25 July 2024.
Valid date format
Invalid date format
YAML
1-type: component.text-field
2instanceId: date-with-month
3options:4label: Date - DD Month YYYY
5errorText: =$contains(@ctx.components.date-with-month.state.value, /^(0[1-9]|[12][0-9]|3[01])\s(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(19|20)\d{2}$/) ? '' :'not a valid date (DD Month YYYY)'6
Expected result: yyyy/mm/dd, e.g. 2024/08/30.
Valid date format
Invalid date format
YAML
1-type: component.text-field
2instanceId: date-year-first
3options:4label: Date - YYYY/MM/DD
5errorText: =$contains(@ctx.components.date-year-first.state.value, /^(19|20)\d{2}\/(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])$/) ? '' :'not a valid date format (YYYY/MM/DD)'6
Expected result: 1234,00
Valid decimal number
Invalid decimal number
YAML
1-type: component.text-field
2instanceId: decimal
3options:4label: Decimal number -111,25errorText: =$contains(@ctx.components.decimal.state.value, /^[0-9]+([,.][0-9]{1,2})?$/) ? '' :'not a decimal number'6
Expected result: H:MM AM/PM e.g. 12:15 AM or 08:45 PM
Valid time format
Invalid time format
YAML
1-type: component.text-field
2instanceId: time
3options:4label: Time - HH:MM AM/PM
5errorText: =$contains(@ctx.components.time.state.value, /^((0?[1-9]|1[0-2]):[0-5][0-9] (AM|PM))$/) ? '' :'not a valid time format, use HH:MM AM/PM'6
Expected result: MM:SS / or HH:MM, e.g. 08:10.
Valid time format
Invalid time format
YAML
1-type: component.text-field
2instanceId: time-23options:4label: Time - MM:SS / or HH:MM
5errorText: =$contains(@ctx.components.time-2.state.value, /^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$/) ? '' :'not a valid time format, use MM:SS / or HH:MM'6
Expected result: 01:00
Valid time format
YAML
1-type: component.text-field
2instanceId: time-33options:4label: Time - 24 hour format
5errorText: =$contains(@ctx.components.time-3.state.value, /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/) ? '' :'not a valid time format, use 24 hour format'6
Expected result: example.com or http://example.com
1-type: component.text-field
2instanceId: isbn
3options:4label: ISBN
5errorText: =$contains(@ctx.components.isbn.state.value, /((978[\--– ])?[0-9][0-9\--– ]{10}[\--– ][0-9xX])|((978)?[0-9]{9}[0-9Xx])/) ? '' :'not a valid ISBN format'6
Expected result: JohnSmith
Valid string -no spaces
Invalid string -no spaces
YAML
1-type: component.text-field
2instanceId: string
3options:4label: String - spaces not allowed
5errorText: =$contains(@ctx.components.string.state.value, /^[a-zA-Z0-9]+$/) ? '' :'not a valid string, spaces are not allowed'6
Expected result: John Smith
Valid string with spaces
Invalid string with spaces
YAML
1-type: component.text-field
2instanceId: string-23options:4label: String - spaces allowed
5errorText: =$contains(@ctx.components.string-2.state.value, /^[a-zA-Z0-9\s]*$/)? '' :'not a valid string, spaces are allowed'6
Expected result: 56575 76 6
Valid numbers with spaces
Invalid numbers with spaces
YAML
1-type: component.text-field
2instanceId: number-spaces
3options:4label: Number and spaces only
5errorText: =$contains(@ctx.components.number-spaces.state.value, /^[0-9\s]*$/)? '' :'not a valid input, use number and spaces only'6
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key
Press space bar to start a drag.
When dragging you can use the arrow keys to move the item around and escape to cancel.
Some screen readers may require you to be in focus mode or to use your pass through key