Ten Regex Expressions To Use With Forms Pro

If you’ve been using Forms Pro to create surveys then you should be aware that you can do custom validation on fields with Regular Expressions (regex). My friend Megan Walker blogged about this recently, showing how easy it is to add the custom formatting checks to a field on your survey. Megan and I were discussing this functionality and, as I’m a massive geek, she asked if we could work together to come up with ten different custom expressions that you could use in a Forms Pro survey; we came up with the following list:

  1. Phone Numbers
  2. Website address
  3. Twitter handle
  4. Zip code (5 digits)
  5. UK Postcode
  6. National Insurance Number (UK)
  7. Social Security Number (USA)
  8. Vehicle Registration (UK)
  9. Date of birth (both dd/mm/yyyy & mm/dd/yyyy)
  10. Time (24 hour & 12 hour AM/PM formats)

I’ll go through each of these in turn below to show you how they work. I’ll include a copy of the expression, an explanation of the key rules that make up the query, a link to Regex101 for testing the query, and a visual overview of how the query works.

UK Phone Number

(\s*\(?(0|\+44)(\s*|-)\d{4}\)?(\s*|-)\d{3}(\s*|-)\d{3}\s*)|(\s*\(?(0|\+44)(\s*|-)\d{3}\)?(\s*|-)\d{3}(\s*|-)\d{4}\s*)|(\s*\(?(0|\+44)(\s*|-)\d{2}\)?(\s*|-)\d{4}(\s*|-)\d{4}\s*)|(\s*(7|8)(\d{7}|\d{3}(\-|\s{1})\d{4})\s*)|(\s*\(?(0|\+44)(\s*|-)\d{3}\s\d{2}\)?(\s*|-)\d{4,5}\s*)

In order to construct any regex query you have to understand the rules of the information you’re trying to validate. Luckily for me, Wikipedia has a great page about UK telephone numbers. Some of the key rules we need to be aware of are:

  1. The numbers can start with +44 (the international dialling code) or 0
  2. The telephone numbers can be prefixed with a 3, 4 or 5 digit area code
  3. 3 digit area code numbers have a format of 3-4-4 (i.e. 000 0000 0000)
  4. 4 digit area codes have a 4-3-4 format (i.e. 0000 000 0000)
  5. 5 digit area codes have either a 5-6 or 5-3-3 format (i.e. 00000 000000 or 00000 000 000)
  6. Area codes may or may not be enclosed in brackets
  7. There are some special case numbers such as Sedbergh and Brampton which have unique area codes (4-2 format area code, followed by 5 or 4 numbers respectively)

As we know the rules, this makes the construction of the regex query much easier, we can take the rules in turn and construct a query for them. In order to demonstrate the query in action I have created a sample on Regex101 that you can use for testing, see https://regex101.com/r/ACqAiu/2

Website Address

^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|www.)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$

the key rules we need to consider for a website address are:

  1. The address may begin with http://, https:// or www.
  2. After the protocol, the address may optionally begin with www.
  3. The web domain may use letters, numbers or hyphens
  4. Hyphens cannot be used at the beginning or end of the domain
  5. The web address typically ends with a top-level domain that is between 2-5 letters (i.e. .com, .net, .gov, etc.)

You can test this expression on Regex101 by clicking here

Twitter Handle

[\@][A-Za-z0-9_]{1,15}$

Twitter usernames have some pretty simple rules:

  1. the username begins with an @ symbol
  2. the username cannot be longer than 15 characters
  3. the username can only be comprised of alphanumeric digits or an underscore

You can test this expression on Regex101 by clicking here

US Zipcode

^[0-9]{5}(?:-[0-9]{4})?$

US Zip Codes are really straightforward, the key rules are:

  1. It can be comprised of 5 numbers
  2. It may optionally also have a 4 digit suffix following a hyphen for the ZIP+4 format

You can test this expression on Regex101 by clicking here

UK PostCode

^(([A-Z]{1,2}[0-9][A-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][A-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[A-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$

The Wikipedia article on UK Postcodes has a great section on validation that covers all of the key rules, and provides the regex that I have inlcuded above. I’d recommend reading it to get an overview of how the validation works.

You can test this expression on Regex101 by clicking here

UK National Insurance Number

^[A-CEGHJ-PR-TW-Z]{2}[0-9]{6}[A-DFM]{1}$

UK National Insurance numbers have some key rules:

  1. They are formatted as two prefix letters, six digits and one suffix letter
  2. Neither of the first two letters can be D, F, I, Q, U or V
  3. The suffix letter can be A, B, C, D, F or M

You can test this expression on Regex101 by clicking here

US SOcial Security Number

^\d{3}(\s*|-)\d{2}(\s*|-)\d{4}$

The rules for US Social Security numbers are:

  1. 9 digits in total
  2. May be in a 3-2-4 format (i.e. 000-00-0000)
  3. The delimiter between the sections of the number may be a hyphen or a white space

You can test this expression on Regex101 by clicking here

UK Vehicle Registration

^([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})|([A-Z]{3}\s?(\d{4})\s?)$

There are a number of rules regarding UK Vehicle Registration numbers, and there is a great gist on GitHub by Daniel Bradley that covers the rules so I’d recommend head there to read about it.

You can test this expression on Regex101 by clicking here

Date of Birth

^(((19|20)\d\d[- \/.](0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01]))|((0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d)|((0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d))$

For a date of birth we want to validate the following rules:

  1. The date may be input in the DD/MM/YYYY, MM/DD/YYYY or YYYY/MM/DD format
  2. The elements of the date may be separated by a forward slash (/), period (.), hyphen (-) or space

You can test this expression on Regex101 by clicking here

TIME – 24 hour Format

^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$

Hopefully the rules around time are obvious, but you can have any time from 00:00 to 24:00, with the hours and minutes separated by a colon. You can optionally include the seconds which will also be separated from the minutes by a colon

You can test this expression on Regex101 by clicking here

TIME – 12 HOUR FORMAT

^([1-9]|1[012]):(0[0-9]|[1-5][0-9])\s?(am|AM|pm|PM)$

Time in a 12 hour format can be anytime from 12:00am to 11:59pm, and may be suffixed with am or pm, which may be in upper or lower case

You can test this expression on Regex101 by clicking here

Conclusion

Hopefully the examples above will give you some ideas for how you can use regular expressions in your own surveys. The examples are intended as a guide, so please test them before implementing them in any production environment. If I have made any glaring errors then please let me know in the comments or by reaching out to me on Twitter or LinkedIn.

If you’d like to know more about Forms Pro then please subscribe to Megan’s blog, follow her on Twitter, watch her YouTube channel or enrol in her excellent online training course.

Published by

3 thoughts on “Ten Regex Expressions To Use With Forms Pro

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s