Useful REGEX sniplets

You are here:
< Back

What is this article about?

3B Forms support custom field validation through the use of Javascript RegExt. This is a simple piece of code which allows you to control the format of the data inputted on your forms. Below, you can find a list of useful/most used RegEx snipplets that you can just add to your forms.

How do I add Custom RegExt to fields?

Just jump over to edit any valid field (Text Fields, Date/Time Fields) from the Form Builder. In the field settings pop-up, right at the bottom, you will see a panel such as this one:

The last option in this panel is called Custom Regular Expression Formula which is where you need to input your RegEx formulas

 

Examples:

1. US Social Security Number

Formula
^(?!219-09-9999|078-05-1120)(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$
Matches
AAA-GGG-SSSS  
  1. The Area Number (A) – This is a three digit number that was used to assign a Social Security number based on geographic location. This could either be the specific office code that the card itself was issued from or based on the zip code that the applicant lived in.
  2. The Group Number (G) – This is a two digit number that ranges from 00-99 and used a specific set of rules to build the sequence from the first value to the last within the group to avoid consecutive values being used.
  3. The Serial Number (S) – This is a straight-forward numeric sequence that is output consecutively from 0000-9999.

 

2. UK National Insurance Number

Formula
^\s*[a-zA-Z]{2}(?:\s*\d\s*){6}[a-zA-Z]?\s*$
Matches
AB 12 34 56 A
AB123456A
AB 123456 A
AB 123 456 A
AB 1 2345 6 A
AB 12 34 56 A

 

3. US Post Code/ZIP Format

Formula
^\d{5}(?:[-\s]\d{4})?$
Matches
  1. 12345
  2. 12345-6789
  3. 12345 1234

 

4. UK Post Code Format

Formula
([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})
Matches
  1. CW3 9SS
  2. SE5 0EG
  3. SE50EG
  4. se5 0eg
  5. WC2H 7LT

 

5. Simple Password Validation

Formula
/^[a-z0-9_-]{6,18}$/
Matches

Matching  6 to 18 letters, numbers, underscores, or hyphens – no special charecters.

 

6. Matching an Email

Formula
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
Matches
We begin by telling the parser to find the beginning of the string (^). Inside the first group, we match one or more lowercase letters, numbers, underscores, dots, or hyphens. I have escaped the dot because a non-escaped dot means any character. Directly after that, there must be an at sign. Next is the domain name which must be: one or more lowercase letters, numbers, underscores, dots, or hyphens. Then another (escaped) dot, with the extension being two to six letters or dots. I have 2 to 6 because of the country specific TLD’s (.ny.us or .co.uk). Finally, we want the end of the string ($).

 

7. Matching a URL

Formula
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
Matches
This regex is almost like taking the ending part of the above regex, slapping it between “http://” and some file structure at the end. It sounds a lot simpler than it really is. To start off, we search for the beginning of the line with the caret.

The first capturing group is all option. It allows the URL to begin with “http://”, “https://”, or neither of them. I have a question mark after the s to allow URL’s that have http or https. In order to make this entire group optional, I just added a question mark to the end of it.

Next is the domain name: one or more numbers, letters, dots, or hypens followed by another dot then two to six letters or dots. The following section is the optional files and directories. Inside the group, we want to match any number of forward slashes, letters, numbers, underscores, spaces, dots, or hyphens. Then we say that this group can be matched as many times as we want. Pretty much this allows multiple directories to be matched along with a file at the end. I have used the star instead of the question mark because the star says zero or more, not zero or one. If a question mark was to be used there, only one file/directory would be able to be matched.

Then a trailing slash is matched, but it can be optional. Finally we end with the end of the line.