# Validating fields with Patterns

Use Regular Expressions to ensure data is entered properly

Some examples of desired validation patterns are checking for currency (two digit decimal values), email addresses, and IP addresses.

Validation patterns can be applied to fields in the validation section.

There are currently no preconfigured patterns, but custom pattern validation can entered (1) along with a message that will displayed if the field fails the validation (2).

Validation patterns are built using regular expressions. Here are some of the symbols/terminology used to put together regular expressions.

Anchors | Classes | Quantifiers | Groups & Ranges | Special Characters |
---|---|---|---|---|

^ Start of string | \s White space | * 0 or more | . Any character except new line (\n) | \n New line |

$ End of string | \S Not white space | + 1 or more | (a | b) a or b |

\d Digit | ? 0 or 1 | [abc] Range (a or b or c) | \r Carriage return | |

\D Not digit | {3} Exactly 3 | [^abc] Not a or b or c | ||

{3,} 3 or more | [a-q] Letter from a to q | |||

{3,5} 3,4, or 5 |

A more complete cheat sheet is available here: http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

The key to setting up regular expressions is knowing your data so that you can model your data.

## Currency Pattern Example

Checking that a number is a two digit decimal (good for currancy) is the equivalent checking that there are any number of numbers, ranging from 0 to 9, before a decimal point and exactly two digits, also ranging from 0 to 9, after the decimal point.

Start | Any Number of digits (0-9) | decimal point (needs to be escaped) | Exactly two digits (0-9) | End |
---|---|---|---|---|

^ | \d* | . | \d{2} | $ |

So the resulting pattern would be: ^\d*.\d{2}$

Note that this would be without a currency symbol (like a dollar sign) and without commas. This would not accept $1.00 or 1,000.00, for example. Say you wanted to allow them to enter a $ or commas in the number. Lets model the number before the decimal point first. If someone uses commas, there can be between 1 and 3 leading digits and there must 3 digits between commas. If they don't use commas, there can be any number of digits.

One to three digits | a comma and then three digits repeated any number of times | Any number of digits |
---|---|---|

\d{1,3} | (,\d{3})* | \d* |

So the pattern for the number before the decimal would be: (\d{1,3}(,\d{3})*|(\d*))

Start | Possible Dollar Sign | Pattern for the number before the decimal | decimal point | Exactly two digits (0-9) |
---|---|---|---|---|

^ | $? | (\d{1,3}(,\d{3})* | (\d*)) | . |

So the resulting pattern would be: ^$?(\d{1,3}(,\d{3})*|(\d*)).\d{2}$

## IP Address Example

IP addresses appear simple, but when you get down to modeling them, they are more complicated than they appear. They are 4 strings of at least one digit, maximum of three separated by a decimal. The minimum for the first number is 1, the minimum for the other numbers are 0. The maximum for each number is 255. These ranges need to be built. The three 0-255 sets are easier to put together because they start at zero. Let's break the 255-0 range down into pieces we can model.

255-250 | 249-200 | 199-0 |
---|---|---|

2 followed by 5 followed by any digit from 0 to 5 | 2 followed by any digit from 0 to 4 followed by any digit 0 to 9 | One digit from 0 to 9 possibly proceeded by a 1 and possibly followed by any digit from 0 to 9 |

25[0-5] | 2[0-4][0-9] OR 2[0-4]\d | 1?[0-9][0-9]? OR 1?\d\d? |

So the resulting group is (25[0-5]|2[0-4][0-9]|[1]?[0-9][0-9]?)

For the next group, the first 255 of the IP, zero isn't really a valid option, so the grouping is a little more complex.

255-250 | 249-200 | 199-100 | 99-1 |
---|---|---|---|

2 followed by 5 followed by any digit from 0 to 5 | 2 followed by any digit from 0 to 4 followed by any digit 0 to 9 | 1 followed by any digit from 0 to 9 followed by any digit from 0 to 9 | Any digit from 1 o 9 possibly followed by any digit from 0 to 9 |

25[0-5] | 2[0-4][0-9] OR 2[0-4]\d | 1[0-9][0-9] OR 1\d{2} | [1-9][0-9]? |

So the resulting group is (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?)

Now to put these together into an IP address.

Start | First Group | decimal | Other Groups | End |
---|---|---|---|---|

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

So the resulting pattern would be: ^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?).(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)$

Other Useful Examples | Pattern Validation |
---|---|

Integer | ^\d+$ |

Decimal | ^-?\d*(.\d+)?$ |

SSN | ^\d{3}-\d{2}-\d{4}$ |

dd-mm-yyyy | ^(((((0[1-9])|(1\d)|(2[0-8]))-((0[1-9])|(1[0-2])))|((31-((0[13578])|(1[02])))|((29|30)-((0[1,3-9])|(1[0-2])))))-((20[0-9][0-9]))|(29-02-20(([02468][048])|([13579][26]))))$ |

Standard Email Address | ^[\w-.][email protected][\w.-]+.[a-zA-Z]{2,4}$ |

Phone: (Ex: 212-555-6666) | ^[2-9]\d{2}-\d{3}-\d{4}$ |

US Zip Code | ^\d{5}([-]\d{4})?$ |

MAC Address | ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$ |

Updated 11 months ago