Category Archives: regular expressions

REGULAR EXPRESSIONS

What are regular expressions?

They’re strings of logic used by computer programmers worldwide, intended to transcend the language barrier for universal application. Professional web developers often use them to validate fields in forms for clients, which is why we’ve worked toward giving you this power as well.

Regular expressions provide a way to match strings of text, which is exactly what Web Form Builder uses them for. They expression given for a field restricts what can be placed in that field by defining what characters can be used, when characters can be used, how many characters can be used, etc. If the information submitted in the field does not match the rules defined in the expression, the submitted information will be rejected, and the visitor will be asked to try again.

Each one of these expressions consists of a bunch of characters which might seem to be a random mess of junk. Actually, those characters define a very specific action. Let’s break down the "Only Letters" preset:

/^[a-z]+$/i

Everything within the slashes (/) is part of the actual expression. The ^ means "begins with." Everything between the brackets ([ and ]) is part of a range, running from the letter a to the letter z. The + sign means that there can be more than one character on this line. The $ means "end of the line." Finally, the i outside of the expression’s slashes is just a mode indicator that states that upper and lower case do not matter.

Put all of that together, and you have an expression that reads:

"This statement matches anything that begins with any letter from A to Z, contains any amount of these letters, contains only letters from A to Z, and they can be both upper and lower case."

Below is a full description of what each preset does. We’ve color-coded some of the different components to help visualize how each expression works.

Alphanumeric

/[0-9a-z]/i

Action: Only numbers and letters can be used in the field; no hyphens, punctuation, special characters, or anything else can be used.

Breakdown: The brackets ([ and ]) declare a range, which includes both 0-9 and a-z. On the end of the phrase, outside of the slashes, is the letter i, which means that the limitation is case insensitive (doesn’t matter whether the letters are upper or lowercase).

Rough English Translation: "Uses any digit from 0 to 9, and any upper or lower case letter from a to z."

Hint: Removing the i at the end would result in just lower case letters being accepted.

Only Numbers

/^[0-9]+$/

Action: Only numbers can be used in the field.

Breakdown: The ^ at the beginning means "starts with"; the brackets ([ and ]) declare a range, which includes the digits 0-9; and the +$ at the end means "until the end of the line."

Rough English Translation: "Starts with any digit from 0 to 9 and only uses digits from 0 to 9 throughout."

Hint: You could also choose a different range of digits here; changing the 9 to a 6 would cause the field to only accept numbers from 0 to 6.

Only Letters

/^[a-z]+$/i

Action: Only letters can be used in the field.

Breakdown: The ^ at the beginning means "starts with"; the brackets ([ and ]) declare a range, which includes the letters a-z; the +$ at the end means "until the end of the line"; and the i outside of the slashes means "case insensitive."

Rough English Translation: "Starts with any upper or lower case letter from a to z and only uses upper and lower case letters from a to z throughout."

Hint: Removing the i at the end would result in just lower case letters being accepted.

No White Spaces

/^[\S]+$/

Action: Will not allow spaces to be used in values typed into this field.

Breakdown: The ^ at the beginning means "starts with"; the brackets ([ and ]) declare a range, which includes the shorthand class \S (which means "not a space"); and the +$ at the end means "until the end of the line."

Rough English Translation: "Starts with anything other than a space, and does not use any spaces throughout."

Bank Routing Number (USA)

/^[0-9]{9}$/

Action: Bank routing numbers are always 9 digits long. Therefore, only numbers can be used, and there must be exactly 9 of them.

Breakdown: The ^ at the beginning means "starts with"; the brackets ([ and ]) declare a range, which includes the digits 0-9; the curly brackets ({ and }) declare a repetition, of which 9 is the declared value; and the $ at the end means "the end of the line."

Rough English Translation: "Starts with any digit from 0-9 and uses exactly 9 of them."

Hint: Changing the {9} to another number will require exactly that many digits to be used.

Bank Account Number (USA)

/^[0-9]{12}$/

Action: Accepts bank account numbers that are 12 digits long. Only numbers can be used, and there must be exactly 12 of them.

Breakdown: The ^ at the beginning means "starts with"; the brackets ([ and ]) declare a range, which includes the digits 0-9; the curly brackets ({ and }) declare a repetition, of which 12 is the declared value; and the $ at the end means "the end of the line."

Rough English Translation: "Starts with any digit from 0-9 and uses exactly 12 of them."

Hint: Change the {12} to the number of digits used in bank account numbers for a specific bank.

eBay Item ID

/^\d{12}$/

Action: All eBay items use a 12 digit ID number. Only numbers can be used, and there must be exactly 12 of them.

Breakdown: The ^ at the beginning means "starts with"; the shorthand class \d means "digits only"; the curly brackets ({ and }) declare a repetition, of which 12 is the declared value; and the $ at the end means "the end of the line."

Rough English Translation: "Starts with any digit and uses exactly 12 of them."

Hint: The range [0-9] can be used in place of the shorthand \d . Both declare the same thing: any digit can be used.

Only Upper Case

/^[A-Z]+$/

Action: Only upper case letters can be used.

Breakdown: The ^ at the beginning means "starts with"; the brackets ([ and ]) declare a range, which includes the letters A-Z;  and the $ at the end means "the end of the line."

Rough English Translation: "Starts with any capital letter from A to Z, and uses only capital letters throughout."

Only Lower Case

/^[a-z]+$/

Action: Only lower case letters can be used.

Breakdown: The ^ at the beginning means "starts with"; the brackets ([ and ]) declare a range, which includes the letters a-z;  and the $ at the end means "the end of the line."

Rough English Translation: "Starts with any lower case letter from A to Z, and uses only lower case letters throughout."

Creating Your Own Custom RegEx

There’s one more option here: Custom. If you’re brave and hardcore, you can supply your own expression or make tweaks to one of the presets. Use the box below the drop-down to enter your custom expression.

You can browse our list of regular expression examples to see if we’ve already collected one that works for you.

This site has a massive archive of expressions. Just make sure to thoroughly test them to ensure that they actually function the way you intend for them to.

More Resources

If you’re creating your own custom expressions, it may be best to use a testing tool to determine that your syntax is correct before using it in your form. Here’s a couple online tools that we think are especially handy:

These sites have a lot more information about regular expressions and how they work:

http://www.coffeecup.com/help/articles/regular-expressions-in-web-form-builder/

Advertisements